Search



Thursday, 26 February 2015

add elements of array

In this program, the task is to take input of n no. of elements in , store it in array & then find the sum of all the elements of array. Here , n is the no. of elements in array & Using for loop , we will get the values in order of a[0] , a[1] , - - - - till a[n] . but while initializing we will add the value entered in a variable named as sum to find sum of all elements in array & then display this value on screen.

#include<stdio.h>
#include<conio.h>
void main()
{ int a[20],i=0,n,sum=0;
clrscr();
printf("\n program to print sum of all elements of array");
printf("\n\n enter no. of elements in array (max 20) : ");
scanf("%d",&n);
printf("\n now enter them : \n");
for(i=0;i<n;i++)
{ printf(" enter a[%d] = ",i);
scanf("%d",&a[i]);
sum = sum + a[i];
}
printf("\n\n sum of all elements of array = %d",sum);
getch();
}

No comments:

Post a Comment

Guys if you think something is wrong or should be edit than please do comment.