How to write a C Program to find Sum of N Numbers using For Loop, While Loop, 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. By using the For loop, this program calculates the sum of N natural numbers.
#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;
}

ANALYSIS:
- In this sum of n numbers program, the first printf statement will ask the user to enter an integer value. And the scanf statement will assign the user entered value to a Number variable.
- Next, we used C Programming 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 C program 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 the sum of n numbers allows the user to enter any integer value. Using the While Loop, we will calculate the sum of N natural numbers.
#include<stdio.h>
int main()
{
int n, i = 1, Sum = 0;
printf("\nPlease Enter any Integer Value\n");
scanf("%d", &n);
while(i <= n)
{
Sum = Sum + i;
i++;
}
printf("Sum of Natural Numbers = %d", Sum);
return 0;
}
Please Enter any Integer Value
5
Sum of Natural Numbers = 15
Within this Sum of N Numbers using the 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 to the 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 C Increment operator.
Program to Calculate Sum of N Numbers using Do While Loop
This sum of n numbers program allows the user to enter any integer value. Using the Do While Loop, we will calculate the sum of N natural numbers. Read more about Do While Loop in C.
#include<stdio.h>
int main()
{
int n, i = 1, Sum = 0;
printf("\nPlease Enter any Integer Value\n");
scanf("%d", &n);
do
{
Sum = Sum + i;
i++;
} while(i <= n);
printf("Sum of Natural Numbers = %d", Sum);
return 0;
}
Please Enter any Integer Value
50
Sum of Natural Numbers = 1275
We just replaced the While loop in the above Sum of N Numbers 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 program allows the user to enter any integer value. Using the Functions, we will calculate the sum of N natural numbers. Refer to Functions in C.
#include<stdio.h>
int SNatNum(int nm);
int main()
{
int nm, Sum = 0;
printf("\nPlease Enter any Integer Value\n");
scanf("%d", &nm);
Sum = SNatNum(nm);
printf("Sum of Natural Numbers = %d", Sum);
return 0;
}
int SNatNum(int nm)
{
int Sum = 0;
if (nm == 0)
{
return nm;
}
else
{
return (nm * (nm + 1) / 2);
}
}
Please Enter any Integer Value
100
Sum of Natural Numbers = 5050
Within this N Numbers program, the following statement will call the SNatNum function and assign the function return value to the Sum variable.
Sum = SNatNum(nm);
The last printf statement will print the Sum as output. Now, let us see the function definition
Within the function, we used the C If Else statement checks whether the Number is equal to Zero or greater than Zero.
- If the given number is equal to Zero then the Sum of N Natural numbers = 0
- Otherwise, we used the mathematical formula of Sum of Series 1 + 2+ 3+ … + N = N * (N + 1) / 2
C Program to find Sum of N Numbers using Recursion
This program to find the sum of n numbers allows the user to enter any integer value. Using the Recursion, we will calculate the sum of N natural numbers. Refer to the Recursion in C.
#include<stdio.h>
int SNatNum(int nb);
int main()
{
int nb, Sum = 0;
printf("\nPlease Enter any Integer Value\n");
scanf("%d", &nb);
Sum = SNatNum(nb);
printf("Sum of Natural Numbers = %d", Sum);
return 0;
}
int SNatNum(int nb)
{
if (nb == 0)
{
return nb;
}
else
{
return (nb + SNatNum(nb - 1));
}
}
Please Enter any Integer Value
1000
Sum of Natural Numbers = 500500
If you observe the above Program to calculate the Sum of N Numbers code, it is very similar to the code we explained in the Functions concept. But within the else statement, we called the function recursively.
return (Number + SNatNum(nb - 1));
Also Read
Comments are closed.