C Programming Code Examples C > For Loops and While Loops Code Examples C program to find sum of even numbers between 1 to n C program to find sum of even numbers between 1 to n Write a C program to input number from user and find sum of all even numbers between 1 to n. How to find sum of even numbers in a given range using loop in C programming. Logic to find sum of even numbers in a given range in C program. Input upper limit to find sum of even number. Store it in some variable say N. Initialize another variable to store sum with 0 say sum = 0. To find sum of even numbers we need to iterate through even numbers from 1 to n. Initialize a loop from 2 to N and increment 2 on each iteration. The loop structure should look like for(j=2; j<=N; j+=2). Inside the loop body add previous value of sum with j i.e. sum = sum + j. After loop print final 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); for(j=2; j<=n; j+=2) { /* Add current even number to sum */ sum += j; } printf("Sum of all even number between 1 to %d = %d", n, sum); return 0; }