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 2 element/number : 7
Enter 3 element/number : 1
Enter 4 element/number : 10
Enter 5 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
*******************************************/
For example:
if user enter no. of elements : 5
then he/she should enter elements value as:
Enter 1 element/number : 2
Enter 2 element/number : 7
Enter 3 element/number : 1
Enter 4 element/number : 10
Enter 5 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
*******************************************/
Figure : Screen-shot for Calculating Sum of Number of elements C program |