In this tutorial, we will discuss C program to calculate sum in array elements
In this topic, we will learn code to how to calculate sum of elements in integer array in C programming language.
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int array[]={45,67,53,47,65,32,45,78};//Array declaration and initiation int sum,count;//variable declaration sum=0; for(count=0; count<=7; count++){//loop iterates to calculate sum sum=sum+array[count]; } printf("Sum of array is %d",sum);//display result on the screen getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Sum of array is 432
In this program, we can see some of the steps by step procedures given below
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int array[5];//Array declaration int sum,i,j;//variable declaration for(i=0; i<=5; i++){ printf("Enter the elements for array[%d]: ",i); scanf("%d",&array[i]);//loop iterates to take input from the user } sum=0; for(j=0; j<=5; j++){//loop iterates to calculate sum sum=sum+array[j]; } printf("\nSum of array elements is %d",sum); getch(); return 0; }
When the above code is compiled and executed, it produces the following results
Enter the elements for array[0]: 56 Enter the elements for array[1]: 78 Enter the elements for array[2]: 98 Enter the elements for array[3]: 75 Enter the elements for array[4]: 46 Enter the elements for array[5]: 76 Sun of array elements is 358
In this program, we can see some of the steps by step procedures given below
Suggested for you
Single dimension array in C language
Data type and variables in C language
For loop in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…