C program to add all elements in Array

C program to add all elements in Array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
 main()
{
   int i,arr[50],sum,num;

   printf("Enter no of elements :");
   scanf("%d",&num);

   //Reading values into Array
   printf("\nEnter the values :\n");
   for(i=0;i<num;i++)
      scanf("%d",&arr[i]);

   //Calculating total
   sum = 0;
   for(i=0;i<num;i++)
      sum=sum + arr[i];

   //Printing all elements from array
   for(i=0;i<num;i++)
      printf("\na[%d]=%d",i,arr[i]);

   //Printing total
   printf("\n\nSum=%d",sum);
}

OUTPUT:

Enter no of elements :5

Enter the values :
7
4
5
1
2

a[0]=7
a[1]=4
a[2]=5
a[3]=1
a[4]=2

Sum=19

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.