C Program to Find Sum of n Natural Numbers
In this article, we are going to write a c program to find the Sum of n Natural Numbers
We will make this program in the following way -:
- C Program to Find Sum of n Natural Numbers (Simple Way – Using For Loop )
- C Program to Find the Sum of n Natural Numbers Using While Loop
- C Program to Find the Sum of n Natural Numbers Using Do While Loop
- C Program to Find the Sum of n Natural Numbers Using Function
- C Program to Find the Sum of n Natural Numbers Using Recursion
Let’s understand all these methods one by one
C Program to Find Sum of n Natural Numbers For Loop
To make this program, you must first know about the loop because we are going to use the concept of loop to make this program.
There are mainly three types of loops – : While Loop, Do While Loop and For Loop.
You can read about loop in detail here -:
Here we are going to make this program with the help of these three types of loops.
Let’s first make this program with the help of simple loop (For Loop)
Algorithm
- Program Start
- Variable Declaration (i,n,sum)
- Input a Number from the User
- Calculating Sum of n Naturals Numbers
- Displaying the Sum of n Naturals Numbers
- Program End
Program
//C Program to Find the Sum of n Natural Numbers Using For loop
#include<stdio.h>
int main()
{
//Declaring Variable
int n, i, sum = 0 ;
//Input Number
printf("Enter a Number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum = sum + i;
}
printf("\nSum of %d Natural Numbers = %d",n, sum);
return 0;
}
Output
Enter a Number
5
Sum of 5 Natural Numbers = 15
Program Explanation
- In this program, we first declared three variables of int data type named i, n, sum.
- After that, we take a number input from the user.
- Then we calculate the sum of n natural numbers.
- after Calculating Sum we print it on the screen through print().
C Program to Find the Sum of n Natural Numbers Using While Loop
Algorithm
- Program Start
- Variable Declaration
- Input a Number from the User
- Calculating Sum of n Naturals Numbers
- Displaying the Sum of n Naturals Numbers
- Program End
Program
//C Program to Find the Sum of n Natural Numbers Using While Loop
#include<stdio.h>
void main()
{
int n, i=1, sum = 0 ;
printf("Enter a Number\n");
scanf("%d",&n);
while(i<=n)
{
sum = sum + i;
i++;
}
printf("\nSum of %d Natural Numbers = %d",n, sum);
}
Output
Enter a Number
10
Sum of 10 Natural Numers = 55
C Program to Find the Sum of n Natural Numbers Using Do While Loop
Program
//C Program to Find the Sum of n Natural Numbers Using Do While Loop
#include<stdio.h>
int main()
{
int n, i=1, sum = 0 ;
printf("Enter A Number to Calculate Sum\n");
scanf("%d",&n);
do
{
sum = sum + i;
i++;
}while(i<=n);
printf("\nSum of %d Natural Numbers = %d",n, sum);
return 0;
}
Output
Enter A Number to Calculate Sum
15
Sum of 15 Natural Numbers = 120
C Program to Find the Sum of n Natural Numbers Using Function
To Calculate the sum n natural numbers through a function, first you need to know about the function. You can read about the function from the link given below.
Algorithm
- Program Start
- Declaring variables
- Input Number
- Calling Function to calculate Sum of n Natural Numbers
- Print the Sum of n Numbers
- Program End
Program
//C Program to Find the Sum of n Natural Numbers Using function
#include<stdio.h>
int sum(int);
void main()
{
int n, i=1, s;
printf("Enter A Number\n");
scanf("%d",&n);
s=sum(n);
printf("\nSum of %d Natural Numbers = %d",n, s);
}
int sum(int n)
{
int i,sum=0;
for(i=1;i<=n;i++)
{
sum = sum + i;
}
return (sum);
}
Output
Enter A Number
20
Sum of 20 Natural Numbers = 210
C Program to Find the Sum of n Natural Numbers Using Recursion
To Calculate the sum n natural numbers through a Recursion, first you need to know about the Recursion. You can read about the Recursion from the link given below.
Algorithm
- Program Start
- Declaring variables
- Input Number
- Calling Recursive Function to calculate Sum of N Natural Numbers
- Print the Sum of n Natural Numbers
- Program End
Program
//C Program to Find the Sum of n Natural Numbers Using Recursion
#include<stdio.h>
int sum(int);
void main()
{
int n, s;
printf("Enter A Number\n");
scanf("%d",&n);
s=sum(n);
printf("\nSum of %d Natural Numbers = %d",n, s);
}
int sum(int n)
{
int s=0;
if(n==1)
return (n);
s = n + sum(n-1);
return (s);
}
Output
Enter a Number
25
Sum of 25 Natural Numbers = 325
Read More
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
So friends, in this article, we have written a program to calculate the sum n natural numbers in five different ways.
- C Program to Find the Sum of n Natural Numbers Using For Loop
- C Program to Find the Sum of n Natural Numbers Using While Loop
- C Program to Find the Sum of n Natural Numbers Using Do While Loop
- C Program to Find the Sum of n Natural Numbers Using Function
- C Program to Find the Sum of n Natural Numbers Using Recursion
If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.