how to sum in c code example

Example 1: sum of array in c

//made by Kashish Vaid
#include <conio.h>
 
 
int main()
{
    int a[1000],i,n,sum=0;
   
    printf("Enter size of the array : ");
    scanf("%d",&n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
 
    
    for(i=0; i<n; i++)
    {
         
        sum+=a[i];
    }
     printf("sum of array is : %d",sum);
 
    return 0;
}
//made by kashish vaid

Example 2: c program to find the sum of given number

# include<stdio.h>
int main
{
int number;
int sum=0;
int remainder;
printf("enter a number\n");
scanf("%d",&number);
for(;number!=0;)
{
remainder=number%10;
sum=sum+remainder;
number=number/10;
}
printf("sum of digits=%d",sum);
}

Tags:

Misc Example