C Program to Find Sum of N Numbers Using Array

Howdy readers, today you will learn how to write a program to find the sum of N numbers using arrays in the C Programming language.

C Program to Find Sum of N Numbers Using Array

The below program prompts the user to enter the size of the array and each element of the array. Then, it computes the sum of all the elements of the array using a simple for loop statement.

So, without any delay, let’s begin this tutorial.

C Program to Find Sum of N Numbers Using Array

C Program

// C Program to Find Sum of N Numbers Using Array
#include <stdio.h>
int main(){
    int num, i, sum = 0;
    int arr[25];
    
    // Taking input
    printf("Enter the size of the array: ");
    scanf("%d", &num);
    
    // Enter array elements
    printf("Enter the elements of the array: \n");
    for (i = 0; i < num; i++){
        scanf("%d", &arr[i]);
    }
    
    // Calculating the sum of the array
    for (i = 0; i < num; i++){
        sum = sum + arr[i];
    }
    
    // Displaying result
    printf("Sum of the array is: %d", sum);
    
    return 0;
}

Output

Enter the size of the array: 5
Enter the elements of the array: 
4
7
11
18
25
Sum of the array is: 65
C Program to Find Sum of N Numbers Using Array

Explanation

    int num, i, sum = 0;
    int arr[25];

In this program, we have declared three int data type variables and one array named num, i, sum and arr[] respectively.

    // Taking input
    printf("Enter the size of the array: ");
    scanf("%d", &num);
    
    // Enter array elements
    printf("Enter the elements of the array: \n");
    for (i = 0; i < num; i++){
        scanf("%d", &arr[i]);
    }

Then, the user is asked to enter the size of the array and elements of the array.

    // Calculating the sum of the array
    for (i = 0; i < num; i++){
        sum = sum + arr[i];
    }

The sum of all the elements of the array is computed using for loop statement.

  • 1st For Loop Iteration: for (i = 0; i < 5; i++), here the condition is True. Therefore, sum = sum + arr[0] = 0 + 4 = 4.
  • 2nd For Loop Iteration: for (i = 1; i < 5; i++), here the condition is True. Therefore, sum = sum + arr[1] = 4 + 7 = 11.
  • 3rd For Loop Iteration: for (i = 2; i < 5; i++), here the condition is True. Therefore, sum = sum + arr[2] = 11 + 11 = 22.
  • 4th For Loop Iteration: for (i = 3; i < 5; i++), here the condition is True. Therefore, sum = sum + arr[3] = 22 + 18 = 40
  • 5th For Loop Iteration: for (i = 4; i < 5; i++), here the condition is True. Therefore, sum = sum + arr[0] = 40 + 25 = 65
  • 6th For Loop Iteration: for (i = 5; i < 5; i++), here the condition is False. So, the loop terminates and we get sum = 65.
    // Displaying result
    printf("Sum of the array is: %d", sum);

Finally, the sum of the elements of the array is printed on the screen using printf() function.

Conclusion

I hope after going through this tutorial, you understand how to write a program to find the sum of N numbers using arrays in C Programming language.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.

Leave a Comment