Programming Languages
Welcome to coding world
C Program to find the sum of natural numbers using while loop
#include <stdio.h>
int main()
{
int n, count, sum
= 0;
printf("Enter
the value of n(positive integer): ");
scanf("%d",&n);
/* When you use
while loop, you have to initialize the
* loop counter
variable before the loop and increment
* or decrement it
inside the body of loop like we did
* for the
variable "count"
*/
count=1;
while(count <=
n){
sum = sum + count;
count++;
}
printf("Sum
of first %d natural numbers is: %d",n, sum);
return 0;
}
Output:
Sum of first 7 natural numbers is: 28