## C Program To Calculate Sum of N Numbers

Learn How To Calculate Sum of N Numbers in C Programming Language. This C Program To Find Addition of N Numbers uses Arrays to store the Elements and finds the Sum of N Integers using a For Loop. Also, we have mentioned a C Program To Add Natural Numbers using Dynamic Memory Allocation in C Programming.

In the second method to Calculate Sum of N Numbers in C Programming, we have used **malloc()** method for Dynamic Memory Allocation that helps to store the User Input.

#### Method 1: C Program To Add N Numbers using Arrays

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main() { int arr[30], count, limit, sum = 0; printf("\nEnter The Total Number of Elements:\t"); scanf("%d", &limit); for(count = 0; count < limit; count++) { printf("\nEnter Element %d:\t", count + 1); scanf("%d", &arr[count]); } printf("\n***Calculating Sum of Elements***\n"); for(count = 0; count < limit; count++) { sum = sum + arr[count]; } printf("\nSum of N Numbers = %d\n", sum); return 0; } |

#### Method 2: Find Addition of N Numbers in C Programming using Dynamic Memory Allocation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<stdio.h> #include<stdlib.h> int main() { int count, limit, *ptr, sum = 0; printf("\nEnter Total Number of Elements:\t"); scanf("%d", &limit); ptr = (int*) malloc (sizeof(int) * limit); for(count = 0; count < limit; count++) { printf("\nEnter Element %d:\t", count + 1); scanf("%d", ptr + count); } printf("\n***Calculating Sum of Elements***\n"); for(count = limit - 1; count >= 0; count--) { sum = sum + (*(ptr + count)); } printf("\nSum of N Numbers = %d\n", sum); return 0; } |

#### Method 3: C Program To Find Sum of N Numbers using Arrays (Single For Loop)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h> int main() { int arr[30], count, limit, sum = 0; printf("\nEnter The Total Number of Elements:\t"); scanf("%d", &limit); for(count = 0; count < limit; count++) { printf("\nEnter Element %d:\t", count + 1); scanf("%d", &arr[count]); sum = sum + arr[count]; } printf("\nSum of N Numbers = %d\n", sum); return 0; } |

#### Output

In case you find any error in the above C Program To Calculate Sum of N Numbers or if you have any doubts, let us know about it in the Comment Section below.

Excellent description for finding sum of natural numbers. Finally i could clear my doubts about DMA and Arrays.

Good to hear that your problem and doubts got cleared. DMA is very easy if tried to understand it properly. Only malloc() function and thats it. It also has some other functions though.