43. C Program to find Sum of n Natural Numbers.
How does this program work?
- In this program your going to learn about how to find Sum of n natural numbers using C-Language.
Here is the code
#include <stdio.h>
int main(void)
{
int n, i, ans = 0;
//Initialize integer
printf("Enter a positive integer: \n");
scanf("%d",&n);
//Using loop to do sum of natural number
for(i=1; i <= n; ++i)
{
ans += i; // ans = ans+i;
}
printf("Sum of %d numbers= %d",n,ans);
return 0;
}