Showing posts with label Difference. Show all posts
Showing posts with label Difference. Show all posts

C Program To Find Sum and Difference Of Two Matrices

How to write a C Program To Find Sum and Difference Of Two Matrices in C Programming Language ?


Solution For C Program :

/*C Program To Find Sum and Difference Of Two Matrices.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[3][2],mat2[3][2];
int i,j;
clrscr();
printf("enter the Ist matrix\n");
for(i=0;i<3;i++)
    {
    for(j=0;j<2;j++)
        {
        scanf("%d",&mat1[i][j]);
        }
    }
printf("\n\n\nFirst Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat1[i][j]);
        }
    }
printf("\n\nEnter the 2nd matrix\n");
for(i=0;i<3;i++)
    {
    for(j=0;j<2;j++)
        {
        scanf("%d",&mat2[i][j]);
        }
    }
printf("\n\n\nSecond Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat2[i][j]);
        }
    }

printf("\n\n Sum of the two matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat2[i][j]+mat1[i][j]);
        }
    }
printf("\n\n Difference of the two matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<2;j++)
        {
        printf("%d\t",mat1[i][j]-mat2[i][j]);
        }
    }
getch();
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

Create Two Singly Linked List Perform Differences Display It C Program

How to write a c program to create two singly linked list and perform differencer of two list and display it in C Programming Language ?

Solution:
/*write a c program to create two singly linked list and perform
differences of two list and display it*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start1=NULL,*start2=NULL,*q1,*q2,*temp1,*temp2;
int n1=0,n2=0,*p1,*p2,i,j;
void create();
void display();
void differ();
void main()
{
clrscr();
create ();
display();
differ();
getch();
}
void create()
{
char ch='y';
while(ch=='y'||ch=='Y')
{
temp1=malloc(sizeof(struct node));
printf("\nenter the number for first L.L:-");
scanf("%d",&temp1->data);
temp1->next=NULL;
n1++;
if(start1==NULL)
start1=temp1;
else
{
q1=start1;
while(q1->next!=NULL)
{
q1=q1->next;
}
q1->next=temp1;
}
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
ch='y';
while(ch=='y'||ch=='Y')
{
temp2=malloc(sizeof(struct node));
printf("\nenter the number for second L.L:-");
scanf("%d",&temp2->data);
temp2->next=NULL;
n2++;
if(start2==NULL)
start2=temp2;
else
{
q2=start2;
while(q2->next!=NULL)
{
q2=q2->next;
}
q2->next=temp2;
}
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
}
void display()
{
p1=(int *)malloc(n1*sizeof(int));
p2=(int *)malloc(n2*sizeof(int));
if(start1==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in first linked list are:-\n");
q1=start1;
for(i=0;i<n1;i++)
{
if(q1!=NULL)
{
*(p1+i)=q1->data;
printf("%d\n",*(p1+i));
q1=q1->next;
}
}
}
if(start2==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in second linked list are:-\n");
q2=start2;
for(i=0;i<n2;i++)
{
if(q2!=NULL)
{
*(p2+i)=q2->data;
printf("%d\n",*(p2+i));
q2=q2->next;
}
}
}
}
void differ()
{
printf("\ndifferences of two sets are:-\n");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(*(p1+i)==*(p2+j))
break;
}
if(j==n2)
printf("%d\n",*(p1+i));
}
}

Output:
/*
enter the number for first L.L:-4                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-8                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-1                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-6                                              
                                                                               
do you want to continue(Y|N):-n                                                
                                                                               
enter the number for second L.L:-4                                             
                                                                               
do you want to continue(Y|N):-y                                                

enter the number for second L.L:-1                                             
                                                                               
do you want to continue(Y|N):-n                                                
                                                                               
element in first linked list are:-                                             
4                                                                              
8                                                                              
1                                                                              
6                                                                              
                                                                               
element in second linked list are:-                                            
4                                                                              
1                                                                              
                                                                               
differences of two sets are:-                                                  
8
6
*/

Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program

How to write C Program a menu driven program to read two integers and find their sum, difference & product in C Programming Language ?



Solution:
/* Write a menu driven program to read two integers & find their sum, difference & product. */

#include<stdio.h>

int main()
{

signed int a[2];
signed int sum,diff,prod;
printf("Enter the two numbers to find sum,difference,product \n");
scanf("%d%d",&a[0],&a[1]);
int option='s';

printf("Enter the character \n 1 -> to find sum \n 2 -> to find difference \n 3 -> to find the product \n ");
scanf("%d",&option);

switch(option)

case 1: {
sum=a[0]+a[1];
printf("the sum of the two numbers is %d \n",sum);
  //break;
}

case 2: {
diff=a[0]-a[1];
printf("the sum of the two numbers is %d \n",diff);
//break;
}

case 3: {
prod=a[0]*a[1];
printf("the sum of the two numbers is %d \n",prod);
//break;
}

default: printf("you have entered the wrong character");

return 0;
}