Program to print the sum of 1st N natural numbers
What are Natural Numbers?
Natural numbers are positive integers starting from 1, 2, 3, and so on. The sum of the first N natural numbers can be calculated using a loop or using the formula:
Sum = n(n+1)/2
#include<stdio.h> int main() { int n,i,sum=0; printf("Enter the limit: "); scanf("%d", &n); for(i=1;i<=n;i++) { sum = sum +i; } printf("Sum of N natural numbers is: %d",sum); } Output Enter the limit: 5 Sum of N natural numbers is 15
Explanation:
-
The user is prompted to enter the value of
N
. -
A
for
loop runs from 1 toN
. -
Each number is added to the
sum
variable. -
Finally, the total sum is displayed.