In this article, we will show you, How to write a C Program to find Sum of N Numbers using For Loop, While Loop and Do While Loop, Functions and Recursion.

## C Program to find Sum of N Numbers using For Loop

This C program allows the user to enter any integer Value. Using the For loop, this c program calculate the sum of N natural Numbers.

/* C Program to find Sum of N Numbers using For Loop */ #include<stdio.h> int main() { int Number, i, Sum = 0; printf("\nPlease Enter any Integer Value\n"); scanf("%d", &Number); for(i = 1; i <= Number; i++) { Sum = Sum + i; } printf("Sum of Natural Numbers = %d", Sum); return 0; }

**OUTPUT**

**ANALYSIS:**

- In this sum of n numbers in c program, first printf statement will ask the user to enter an integer value and the scanf statement will assign the user entered value to Number variable.
- Next, we used For Loop to iterate between 1 and user entered value. If you don’t know the For Loop then please refer For loop in C Programming article for further reference.
- Within the For loop, we calculated the sum
- In the above example, user entered value is 5 it means, 1 + 2 + 3 + 4 + 5 = 15

## C Program to find Sum of N Numbers using While Loop

This program for sum of n numbers in c allows the user to enter any integer Value. Using the While Loop we will calculate the sum of N natural Numbers in c.

/* C Program to find Sum of N Numbers using While Loop */ #include<stdio.h> int main() { int Number, i = 1, Sum = 0; printf("\nPlease Enter any Integer Value\n"); scanf("%d", &Number); while(i <= Number) { Sum = Sum + i; i++; } printf("Sum of Natural Numbers = %d", Sum); return 0; }

**OUTPUT**

**ANALYSIS**

Within this C Program to find Sum of N Numbers using while loop example,

- While loop is used to iterate between 1 and user entered value. if you don’t know the While Loop then please refer While Loop in C article for further reference.
- Within the while loop, we calculated the sum
- After completing this, the value of i is increment by the Increment operator.

## C Program to find Sum of N Numbers using Do While Loop

This sum of n numbers in c program allows the user to enter any integer Value. Using the Do While Loop we will calculate the sum of N natural Numbers.

/* C Program to find Sum of N Numbers using Do While Loop */ #include<stdio.h> int main() { int Number, i = 1, Sum = 0; printf("\nPlease Enter any Integer Value\n"); scanf("%d", &Number); do { Sum = Sum + i; i++; } while(i <= Number); printf("Sum of Natural Numbers = %d", Sum); return 0; }

**OUTPUT**

We just replaced the While loop in the above Sum of N Numbers in C program with the Do While loop. Please don’t forget to miss the semi-colon after the while condition.

## C Program to find Sum of N Numbers using Functions

This sum of n numbers in c program allows the user to enter any integer Value. Using the Functions we will calculate the sum of N natural Numbers.

/* C Program to find Sum of N Numbers using Functions */ #include<stdio.h> int Sum_Of_Natural_Numbers(int Number); int main() { int Number, i, Sum = 0; printf("\nPlease Enter any Integer Value\n"); scanf("%d", &Number); Sum = Sum_Of_Natural_Numbers(Number); printf("Sum of Natural Numbers = %d", Sum); return 0; } int Sum_Of_Natural_Numbers(int Number) { int i, Sum = 0; if (Number == 0) { return Number; } else { return (Number * (Number + 1) / 2); } }

**OUTPUT**

**ANALYSIS**

Within this C Program to find Sum of N Numbers, First, we declared the function before the main function. The first printf statement will ask the user to enter an integer value and the scanf statement will assign the user entered value to Number variable.

Following statement will call the Sum_Of_Natural_Numbers function and assign the function return value to the Sum variable.

Sum = Sum_Of_Natural_Numbers(Number);

Last printf statement will print the Sum as output. Now, let us see the function definition

Within the function, we used the If Else statement checks whether the Number is equal to Zero or greater than Zero.

- If the given number is equal to Zero then Sum of N Natural numbers = 0
- Otherwise, we used the mathematical formula of Sum of Series 1 + 2+ 3+ … + N = N * (N + 1) / 2

**TIP:** If the function is not returning any value then used Void

## C Program to find Sum of N Numbers using Recursion

This program to find sum of n numbers in c allows the user to enter any integer Value. Using the Recursion we will calculate the sum of N natural Numbers.

/* C Program to find Sum of N Numbers using Recursion */ #include<stdio.h> int Sum_Of_Natural_Numbers(int Number); int main() { int Number, Sum = 0; printf("\nPlease Enter any Integer Value\n"); scanf("%d", &Number); Sum = Sum_Of_Natural_Numbers(Number); printf("Sum of Natural Numbers = %d", Sum); return 0; } int Sum_Of_Natural_Numbers(int Number) { if (Number == 0) { return Number; } else { return (Number + Sum_Of_Natural_Numbers(Number - 1)); } }

**OUTPUT**

**ANALYSIS:**

If you observe the above C Program to find Sum of N Numbers code, it is very similar to the code we explained in Functions concept but within the else statement we called the function recursively

return (Number + Sum_Of_Natural_Numbers(Number - 1));

Thank you for visiting our Blog