C Program to Find Sum of Natural Numbers Using While Loop. In order to find sum we need to iterate through all natural numbers between 1 to n. Initialize a loop from 1 to N, increment loop counter by 1 for each iteration.
C Program to Find Sum of Natural Numbers Using While Loop
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a Positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}
Output of Program
Enter a Positive integer: 756
Sum = 286146