C program to find the sum of ‘n’ numbers using array

This program below will print the sum of all the elements in the array. Firstly, the program will ask for the number of elements that the user wants to sum in order to allocate the space for the array. Then the program will provide the user to enter all elements one by one using the loop.  The sum of all the elements will be displayed as an output by the program.

Loading...

For understanding this topic more clearly you must have the prior knowledge of the following topic.

Code:

#include<stdio.h>
int main(){
	int array[100], n, b, i, sum=0;
	printf("How many elements do you want to sum? ");
	scanf("%d",&n);

printf("Enter the element: ",n);
	        for(i=1; i<=n; i++){
		scanf("%d", &array[i]);
		sum = sum + array[i];
	}
	printf("Sum = %d", sum);
}

Following is the output generated by the program above.

Output:

How many elements do you want to sum? 3
Enter the element: 1
2
3
Sum = 6
Loading...
SHARE C program to find the sum of ‘n’ numbers using array

Leave a Reply

Your email address will not be published. Required fields are marked *

Share