C Program to find sum of two numbers



#include<stdio.h>
#include<conio.h>
void main(){
int arr[10],sum=0,i;
clrscr();
printf("\nEnter elements of array:");
for(i=0;i<10;i++)
{
printf("\nEnter element %d:",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<10;i++)
{
sum=sum+arr[i];
}
printf("Sum of all elements in array is:%d",sum);
getch();
}

Explanation:

The above program will calculate the sum of elements of an array. There is an array of ten elements. There is a 'for' loop which is executing from 0 to 9 and calculating the sum of all elements of array. We can understand this process by using following example:

arr[0]=0,arr[1]=1,arr[2]=2,arr[3]=0,arr[4]=1,arr[5]=2,arr[6]=0,arr[7]=1, arr[8]=2,arr[9]=0
sum=0
i=0;
i<10,True
iteration1:
sum=sum+arr[0]=0+0=0
i=i+1=1
i<10,True
iteration 2:
sum=sum+arr[1]=0+1=1
i=i+1=2
i<10,True
iteration 3:
sum=sum+arr[2]=1+2=3
i=i+1=3
i<10,True
iteration 4:
sum=sum+arr[3]=3+0=3
i=i+1=4
i<10,true
iteration 5:
sum=sum+arr[4]=3+1=4
i=i+1=5
i<10,True
iteration 6:
sum=sum+arr[5]=4+2=6
i=i+1=6
i<10,True
iteration 7:
sum=sum+arr[6]=6+0=6
i=i+1=7
i<10,true
iteration 8:
sum=sum+arr[7]=6+1=7
i=i+1=8
i<10,true
iteration 9:
sum=sum+arr[8]=7+2=9
i=i+1=9
i<10,true
iteration 10:
sum=sum+arr[9]=9+0=9
i=i+1=10
i<10,false
loop break;
so finally sum of elements of array is :9
The output of the above program is given below:

Output:

c program