Write A C Program to Find Sum of Natural Numbers Using While Loop
Logic :
What is Natural Number ?
Natural Numbers Are Positive numbers Start With 1 Not 0 ( Zero )
So idea is simple use loop ans initialize with 1 and run into last terms ( Till you Want ) ,you can also print the sum of natural number using formula there are 2 method for solve.
#include<stdio.h>
int main()
{
int n, count=1, sum=0;
while(1)
{
printf("\nEnter The Natural Number :\n");
scanf("%d",&n);
while(count<=n)
{
sum+=count;
++count;
}
printf("\nSum Of Natural Number Is = %d\n",sum);
}
return 0;
}
Output:
Enter The Natural Number :
10
Sum Of Natural Number Is=55
Enter The Natural Number:
20
Sum Of Natural Number Is=210
need an explanation for this answer? contact us directly to get an explanation for this answer