C program and flowchart to print sum of n natural numbers

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:

  1. START
  2. Read the value of n
  3. Add n to the sum variable until the value of n is greater than that
  4. Decrease the value of n by 1
  5. Write the final value of n
  6. 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:

C Program to print Sum of N Natural numbers
groot
groot

Leave a Reply

Your email address will not be published. Required fields are marked *