How to find sum of n elements in an array in c programming | Sum of n natural numbers in an array

 Here, we will learn how to find the sum of n elements which are stored in the array in c programming. 

sum of n natural numbers using array in c
sum of n natural numbers using array in c 


Lets start with an example on how to find the sum of all the elements 

Say we have an array of integers of size 5. i.e int arr[5] which has the elements 1 to 5 

int arr[5] = {1, 2, 3, 4, 5};

So now we have to add each element present in an array, we get sum = 1+2+3+4+5 = 15.

Let's consider another example,

Int arr[3] = {5, 10, 10}; and we get sum = 5 + 10 + 10 = 25.


Algorithm:-

  1. Start.
  2. Input n which is the size of the array.
  3. Input n elements from the user one by one and store the elements in an array.
  4. Compute sum by using for loop.
  5. After computing the sum print the sum on the screen.
  6. Stop.

Program using for-loop:-

In this program, first we declare the integer variable n which is the size of the array and then we are taking the input n from the user. After taking the input from the user we are checking if n is less than or equal to 0, if yes then we print it is an invalid number if n is greater than zero than we continue with the program. We then initialize the array with n size and the integer variable sum to zero which will store the final sum. Run the for-loop to input elements from the user and store the elements in an array. Run another for-loop to compute the sum and after computing the sum print it on the screen. 

//c program to calculate the sum of all elements in an array using for loop
#include <stdio.h>
#include <stdlib.h>

int main()
{
    // inputing n from the user where n is the size of the array 
    int n;
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    
    //checking if the number entered is valid or not. If its not valid then we exit the program else 
    //we continue with the program
    if(n<=0) {
        printf("Please enter the valid number");
        exit(0);
    }
    
    // Declaring the arra with size of n and inintializing the variable sum to zero.
    int arr[n];
    int sum=0;

    //Taking the input in the array from the user
    printf("Enter the elements one by one in the array: \n"); 
    for (int i = 0; i < n; i++) {
        printf("Enter element: ");
        scanf("%d", &arr[i]);
    }
    
    //computing the sum 
    for (int i = 0; i < n; i++) {
        sum = sum + arr[i];
    }
    
    printf("The sum of all elements in an array is: %d\n", sum);
    
    return 0;
}

Output:-
Enter the size of the array: 5
Enter the elements one by one in the array: 
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 5
The sum of all elements in an array is: 15

Output:-
Enter the size of the array: 10
Enter the elements one by one in the array: 
Enter element: 3
Enter element: 5
Enter element: 2
Enter element: 6
Enter element: 0
Enter element: 13
Enter element: 56
Enter element: 23
Enter element: 1
Enter element: 17
The sum of all elements in an array is: 126
Program using while-loop:-

//c program to calculate the sum of all the elements in an array using while-loop
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    
    //checking if the number entered is valid or not
    if(n<=0) {
        printf("Please enter the valid number");
        exit(0);
    }
    
    int arr[n];
    int sum=0;
    int i = 0;
    
    //Taking the input in the array from the user
    printf("Enter the elements one by one in the array: \n"); 
    while (i < n) {
        printf("Enter element: ");
        scanf("%d", &arr[i]);
        i++;
    }
    
    i=0;
    //computing the sum 
    while ( i < n) {
        sum = sum + arr[i];
        i++;
    }
    
    printf("The sum of all elements is: %d\n", sum);
    
    return 0;
}

Ouput:-
Enter the size of the array: 5
Enter the elements one by one in the array: 
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Enter element: 5
The sum of all elements is: 15

Output:-
Enter the size of the array: 10
Enter the elements one by one in the array: 
Enter element: 34
Enter element: 23
Enter element: 87
Enter element: 12
Enter element: 7
Enter element: 9
Enter element: 54
Enter element: 32
Enter element: 45
Enter element: 33
The sum of all elements is: 336

Post a Comment

0 Comments