9.26.2013

Calculating Sum of Number of elements

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
*******************************************/


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

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. sir i was pertaining of an alternate addition and subtraction example 1+2-3+4-5= -1
    something like that :) thank you :) sorry for the misunderstanding

    ReplyDelete