Natural number
Natural numbers are numbers that are common and clear in nature. As such, it is a whole, non-negative number.
Logic
To print the sum of first N natural numbers, we need to run one loop from 1 to N, and each time inside the loop we have to add / sum value of “i” (current number) into one temporary variable.
Once the loop is over, outside of the loop we have to print a temporary variable containing the sum of natural numbers.
See also: Print first N natural numbers using for loop
Program
#include <stdio.h> int main() { int i, num, sum = 0; // Take number from user printf("Enter any number : "); scanf("%d", &num); for(i = 1; i <= num; i++) { sum += i; } printf("\nSum of first %d natural numbers = %d", num, sum); return 0; }
Output
Enter any number : 50
Sum of first 50 natural numbers = 1275