Natural numbers are the counting numbers that start from 1 and go up to infinity. They do not include 0 or any negative numbers. In this C Program example, we will write a C program and flowchart to print sum of n natural numbers.
To begin with, we must have knowledge of the following concepts:
- C Operators
- While Loop
Algorithm to Print Sum of N Natural numbers
Here’s the algorithm to print sum of n natural numbers:
- START
- Read the value of n
- Add n to the sum variable until the value of n is greater than that
- Decrease the value of n by 1
- Write the final value of n
- STOP
C Program to print Sum of N Natural numbers
Here’s the C Program to print Sum of N Natural numbers:
/* Write a C program and flowchart to print sum of n natural number */
#include <stdio.h>
void main()
{
int n, sum;
printf("Enter the natural number");
scanf("%d", &n);
while(n>0)
{
sum=sum+n;
n--;
}
printf("%d",sum);
}
Program Output
Here’s the output of the above program:
