Sum of the elements of an array in C
By: Ignatius
This simple C program computes the sum of all the elements of an array.
#include <stdio.h>
#define SIZE 12
int main()
{
// declare and initialize the array named a with size SIZE
int a[SIZE] = {1,3,5,4,7,2,99,16,45,67,89,45};
// declare two normal variables
int i, total = 0;
// do the loop for the array...
for(i = 0; i <= (SIZE-1); i++)
{
// display the array and its element...
printf("\n a[%d]= %d", i, a[i]);
// total up the array
// total = total + a[i]
total += a[i];
}
printf("\nThe sum of the array elements is %d\n", total);
return 0;
}
Archived Comments
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C