A program to find the sum of "n" natural numbers (WHILE)
C Programming Language 031
The following code shows how to write a program to find the sum of 'n' natural numbers (WHILE).
Program
#include <stdio.h>
void main()
{
int i = 1, sum = 0, n;
printf("Enter a value for n: ");
scanf("%d", &n);
while (i <= n)
{
sum = sum + i;
i++;
}
printf("Sum of %d natural numbers = %d", n, sum);
printf("\n");
}
Result
Enter a value for n: 9
Sum of 9 natural numbers = 45
Last Updated on
Comments