Example
Input
Input upper limit: 10
Output
Sum of odd numbers from 1-10: 25
/**
* C program to print the sum of all odd numbers from 1 to n
*/
#include <stdio.h>
int main()
{
int i, 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(i=1; i<=n; i+=2)
{
sum += i;
}
printf("Sum of odd numbers = %d", sum);
return 0;
}
Program to find sum of odd numbers in given range
/**
* C program to print the sum of all odd numbers in given range
*/
#include <stdio.h>
int main()
{
int i, start, end, sum=0;
/* Input range to find sum of odd numbers */
printf("Enter lower limit: ");
scanf("%d", &start);
printf("Enter upper limit: ");
scanf("%d", &end);
/* If lower limit is even then make it odd */
if(start % 2 == 0)
{
start++;
}
/* Iterate from start to end and find sum */
for(i=start; i<=end; i+=2)
{
sum += i;
}
printf("Sum of odd numbers between %d to %d = %d", start, end, sum);
return 0;
}
/*
Output :
Enter lower limit: 4
Enter upper limit: 11
Sum of odd numbers between 4 to 11 = 32
*/