Share on Google+Share on Google+

Sum of array element in C

Advertisement
In this tutorial you will know how to get the sum of each element of an array. There are two function in this given example one for reading the array element and other for writing on console.

Code:

#include<stdio.h>
#include<conio.h>
void main()
 {
  void read(int *,int);
  void display(int *,int);
  int a[5],i,sum=0;

  clrscr();
  printf("Enter five elements for an array \n");
  read(a,5);
  printf("The list elements are  \n");
  display(a,5);
  for(i=0;i<5;i++)
   {
	sum+=a[i];
   }
  printf("The sum of the elements of the array  is %d\n",sum);
  getch();
 }

 void read(int c[],int i)
  {
   int j;
   for(j=0;j<i;j++)
	scanf("%d",&c[j]);
   fflush(stdin);
  }

  void display(int d[],int i)
  {
   int j;
   for(j=0;j<i;j++)
	printf("%d  ",d[j]);
	printf("\n");
  }

Output:


Download this code

Advertisements

Advertisement

Posted on: April 30, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.