C Programming

C Programming


Calculating Sum of Number of elements

Posted: 25 Sep 2013 12:04 PM PDT

Q. Write a C program to accept the number of elements to be insert from the user and calculate these elements sum.

For example:

if user enter no. of elements : 5
then he/she should enter elements value as:
Enter 1 element/number : 2
Enter 1 element/number : 7
Enter 1 element/number : 1
Enter 1 element/number : 10
Enter 1 element/number : 50
Result will be : 
Sum of elements/numbers : 70

Ans.

/*c program for entered number of elements and calculate the sum*/
#include<stdio.h>
int main()
{
 int num,arr[20],i,sum=0;
 //arr[20] value of changeable as big value

 printf("Enter number of element : ");
 scanf("%d", &num);
 for(i=1; i<=num; i++)
 {
    printf("Enter %d element : ",i);
    scanf("%d", &arr[i]);
 }
 for(i=1; i<=num; i++)
    sum = sum + arr[i];
 printf("\nSum of all numbers : %d",sum);
 getch();
 return 0;
}

/******************************************
The output of above program would be
*******************************************/


Output of Calculating Sum of Number of elements C program
Figure : Screen-shot for Calculating Sum of Number of
 elements C program



Post a Comment