Calculations

C program to calculate sum in array elements

C program to calculate sum in array elements

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.

Calculate the sum of array elements

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

  1. Create an array and define its values
  2. Define essential variables with initialization
  3. for loop iterates to each value to the variable
  4. Add each element of the sum variable from the array
  5. finally, display result on the screen

Calculate the sum of array elements – takseinput from user

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

  1. Create an array and define its values
  2. Define neediest variables with initialization
  3. for loop iterate to each value for getting the input valuef
  4. or loop iterate each value for calculating the variable
  5. Add each element of the sum variable from the array
  6. finally, display sum on the screen

 

Similar post

 largest number of three number in C

 largest number of three number in C++

 largest number of three number in Python

 largest number of three number in C using function

 largest number of three number in C++ using function

 largest number of three number in Python using function

 largest number of three number in Java using method

 

Suggested for you

Operator in C language

Single dimension array in C language

Data type and variables in C language

For loop in C language

Cpp Program to calculate average of array
Python program to add two number using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago