C Programming Code Examples C > For Loops and While Loops Code Examples find sum of natural numbers from 1 to n find sum of natural numbers from 1 to n Write a C program to find the sum of all natural numbers between 1 to n using for loop. How to find sum of natural numbers in a given range in C programming. Logic to find sum of all natural numbers in a given range in C programming. Input upper limit to find sum of natural numbers. Store it in some variable say N. Initialize another variable to store sum of numbers say sum = 0. 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. The loop structure should look like for(j=1; j<=N; j++). Inside the loop add previous value of sum with j. Which is sum = sum + j. Finally after loop print the value of sum. #include <stdio.h> int main() { int j, n, sum=0; /* Input upper limit from user */ printf("Enter upper limit: "); scanf("%d", &n); /* Find sum of all numbers */ for(j=1; j<=n; j++) { sum += j; } printf("Sum of first %d natural numbers = %d", n, sum); return 0; }