C Program for Sum of Numbers in an One-Dimensional Array

The below code will generate the sum of all the numbers stored in an 1D-Array.

Logic: Add all the numbers from index zero to the last index

Code:

#include <stdio.h>
#include <conio.h>

void main()
{
   int i,sum;
   sum =0;
   int numbers[]={1,5,2,3,4,7};

  for(i=0;i<6;i++)
  {
     sum=sum+numbers[i];
  }
   
  printf("Sum is %d",sum);

 printf("\npress any key to continue");
 getch();
}

Post a Comment

CodeNirvana
Newer Posts Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top