Learn about Sum of odd numbers within a range in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Sum of odd numbers within a range
The below C program finds the sum of all the odd numbers in the range given by the user. The for loop is used in the below program to the sum.
Program:
#include<stdio.h>
int main()
{
int minRange, maxRange, sum=0;
printf("Enter min and max value of the range: ");
scanf("%d %d",&minRange, &maxRange);
for(int i=minRange; i<=maxRange; i++)
{
if(i%2!=0) sum += i;
}
printf("Sum of odd numbers from %d to %d is: %d", minRange, maxRange, sum);
return 0;
}
Output:
Enter min and max value of the range: 1 100
Sum of odd numbers from 1 to 100 is: 2500
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Sum of first N odd numbers without using a loop
Sum of First Odd Numbers in C from 1 to N