C Programming Code Examples C > For Loops and While Loops Code Examples program to find sum of odd numbers from 1 to n program to find sum of odd numbers from 1 to n Write a C program to find sum of all odd numbers from 1 to n using for loop. How to find sum of all odd numbers in a given range in C programming. Logic to find sum of odd numbers in a given range using loop in C programming. Input upper limit to find sum of odd numbers from user. Store it in some variable say N. Initialize other variable to store sum say sum = 0. To find sum of odd numbers we must iterate through all odd numbers between 1 to n. Run a loop from 1 to N, increment 1 in each iteration. The loop structure must look similar to for(j=1; j<=N; j++). Inside the loop add sum to the current value of j i.e. sum = sum + j. Print the final value of sum. #include <stdio.h> int main() { int j, n, sum=0; /* input range to find sum of odd numbers */ printf("Enter upper limit: "); scanf("%d", &n); /* Find the sum of all odd number */ for(j=1; j<=n; j+=2) { sum += j; } printf("Sum of odd numbers = %d", sum); return 0; }