Showing posts with label Sum. Show all posts
Showing posts with label Sum. Show all posts

20/02/2019

Sum Of Two Matrix in C-Programming:

 We will add two matrix in C-Programming. First we have to know that how to add two matrix. So two matrix can be added if the order of this two matrix are same. Now how the addition is defined? - the matrix addition is defined as in the given picture.
Sum Of Two Matrix in C-Programming

Important Tools:

1. array
2. for-loop
3. if-else

Code:

Sum Of Two Matrix in C-Programming

#include<stdio.h>
#include<conio.h>
int main()
{
int m,n,m1,n1,a[50][50],i,j,b[50][50],c[50][50];
printf("Enter the order of the first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter the order of the second matrix: ");
scanf("%d%d",&m1,&n1);
//Condition for addition
if(m!=m1||n!=n1)
printf("The sum is not possible");
//The first matrix scan and print
else
{
printf("Enter the first matrix row wise: ");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    scanf("%d",&a[i][j]);
  }
}
printf("The 1st matrix is:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
  printf("%d\t",a[i][j]);
  }
printf("\n");
}
//the second matrix scan and print
printf("Enter the 2nd matrix row wise:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    scanf("%d",&b[i][j]);
  }
}
printf("The 2nd matrix is:\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
  printf("%d\t",b[i][j]);
  }
printf("\n");
}
//addition of two matrix print
printf("The sum of the matrix\n");
for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
  {
    c[i][j]=a[i][j]+b[i][j];
  printf("%d\t",c[i][j]);
  }
  printf("\n");
}
}
}

Output:

The output is given below
Sum Of Two Matrix in C-Programming

Explanation:

1. We use <stdio.h> and <conio.h> two header package.
2. first of all we check the condition for the addition of two matrix. So we use if-else condition for that. If the order of two matrix is equal then it can be added if not it is impossible.
3. We use  m,n,m1,n1,a[50][50],i,j,b[50][50] and c[50][50] variable here. m and n are used for the order of the 1st matrix and n1 and  m1 are used for order of 2nd matrix. a[50][50] and b[50][50] are used for store the given two matrices and we used c[50][50] to store and show the sum of two given matrices. And i and j are used in the loop.
4. First we scan and store two matrices in the variable a[50][50] and b[50][50] respectively using for-loop for know more see my post. After that we add the two matrices and store it in the variable c[50][50]. Therefore we print the c[50][50] variable using for-loop.

16/02/2019

Sum of n real numbers using array: 

Here we want to sum the numbers using array. you can give many numbers and add them by array. We introducing here the array syntax. For example 1+2+3+2+5. First we will give the code and after that we will explain.
Sum of n real numbers using array

Code:

Sum of n real numbers using array input

#include<stdio.h>
int main()
{
  int i,n;
  float a[30],sum=0;
  printf("How many numbers you want to add:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  scanf("%f",&a[i]);
  for(i=0;i<n;i++)
  sum=sum+a[i];
  printf("The sum of the given numbers is %.2f",sum);
}

Output:

The output is given below:
Sum of n real numbers using array

Explanation: 

1. We first use <stdio.h> header package.
2. we use two integers variable i and n and two float variable a[30] and sum.
3. we use n for how many numbers users want to do sum and i is for for-loop. we use a[30] array variable to store the given numbers and we use sum variable to sum all the given numbers. Here you can change the array size by replacing the number 30. To know more about use of array variable visit syntax page.
4. We use two for-loop. First is for take all the given value from the users and save it in the array variable a[30]. Second for-loop for doing the sum of all the numbers. To know more about for-loop visit syntax package.
5. After that we print the sum and we use the syntax "%.2f" for showing 2 numbers after point in sum, like 12.35 or 4.20. You can use .3 or .4 as your wish for the changing. If you use .3 then it will like 1.235 or 5.125.


                                           If you have any doubts please comment below.  

11/02/2019

Sum of two Real numbers in C-programming: 

We are going to do sum of two real numbers in C-programming. Here you can use any C-programming software to do it. At first we give the code of this programming and then we explain it smoothly. We always try to make easy for the beginners.

                                                                                                                                                                        The code of this programme is below:                                                                   
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,add;
printf("Enter two numbers:\n");
scanf("%f%f",&a,&b);
add=a+b;
printf("the sum of %f and %f is %f",a,b,add);
getch();
}
If you run the above program then you get this output below:




EXPLANATION:

1. First of all we used two packages <stdio.h> and <conio.h>. to know the uses of <stdio.h> and <conio.h> visit our packages page.
2. we used "void main()" here you can use only "main()" if you use windows operating system or you can use "int main()" if you use Linux operating system.
know more in important syntax of our page.
3. we used three float variable a,b,add.
4. Now we use "a" and "b" for store the two given numbers and use "add" variable to give the addition value of "a" and "b".
5. Now we print the value of "add" variable and we get the output.
6. Now we use "getch()". To know more go to important syntax

09/02/2019

Sum of two number(integers) in C: We use C-programming to add two numbers (integers). We use Borland C++ to compile the programming code.
Sum of two number
At first we write the code and then we discuss how it will work then we will upload the picture of outcome results of this programming.
The code is --

#include<stdio.h>          #include<conio.h>
void main()
{
int a,b,add;
printf("Enter the two number:\n");
scanf("%d%d",&a,&b);
add=a+b;
printf("the sum of of %d and %d is %d",a,b,add);
getch();
}

        here we use a package stdio.h to run the C- program here i use three integers variable 'a' ,'b' and 'add' . At first we use 'a' and 'b' to store two given number(integer) and after saving this two numbers we add this and save the number in variable 'add' and after saving the value of add we printed it. We use getch() command to stable the output window. If you use Linux operating system then you have not use the command getch().


                                The output result of this C-programming is in below the video: