Write a C program to find the sum of n numbers.
int main() {
int i, n, sum = 0, value;
/* get the no of inputs from the user */
printf("Enter the value for n:");
scanf("%d", &n);
/* find the sum of given n inputs */
for (i = 0; i < n; i++) {
printf("Value[%d]: ", i);
scanf("%d", &value);
sum = sum + value;
}
/* print the sum of given n numbers */
printf("sum of given %d numbers is %d\n", n, sum);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for n:10
Value[0]: 11
Value[1]: 19
Value[2]: 10
Value[3]: 15
Value[4]: 20
Value[5]: 15
Value[6]: 10
Value[7]: 20
Value[8]: 30
Value[9]: 40
sum of given 10 numbers is 190
Enter the value for n:10
Value[0]: 11
Value[1]: 19
Value[2]: 10
Value[3]: 15
Value[4]: 20
Value[5]: 15
Value[6]: 10
Value[7]: 20
Value[8]: 30
Value[9]: 40
sum of given 10 numbers is 190
No comments:
Post a Comment