How to write a C Program to Print Sum of Odd Numbers with an example?.

## C Program to Print Sum of Odd Numbers from 1 to n

This program allows the user to enter the maximum limit value. Next, this c program calculates the sum of odd numbers between 1 and the maximum limit value

**TIP:** We already explained the logic to check whether the given is Even or Not in C Program to Check Odd or Even article.

/* C Program to Print Sum of Odd Numbers from 1 to N */ #include<stdio.h> int main() { int i, number, Sum = 0; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Odd Numbers between 0 and %d are : ", number); for(i = 1; i <= number; i++) { if ( i%2 != 0 ) { printf("%d ", i); Sum = Sum + i; } } printf("\n \n The Sum of Odd Numbers from 1 to %d = %d", number, Sum); return 0; }

**OUTPUT**

**ANALYSIS**

Within this Program to Print Sum of Odd Numbers from 1 to n example, For Loop will make sure that the number is between 1 and maximum limit value.

for(i = 1; i <= number; i++)

In the Next line, We declared the If statement

if ( number % 2 != 0 )

Any number that is not divisible by 2 is odd. If condition will check whether the remainder of the number divided by 2 is not equal to 0 or not. If the condition is True, then it is an Odd number, and the C Programming compiler will add i value to sum.

## C Program to Print Sum of Odd Numbers from 1 to n

This program to find the Sum of Odd Numbers in C is the same as above, but we altered for loop to eliminate the If statement.

/* C Program to Print Sum of Odd Numbers from 1 to N */ #include<stdio.h> int main() { int i, number, Sum = 0; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Odd Numbers between 0 and %d are : ", number); for(i = 1; i <= number; i=i+2) { Sum = Sum + i; printf("%d ", i); } printf("\n \n The Sum of Odd Numbers from 1 to %d = %d", number, Sum); return 0; }

**OUTPUT**

## Program to Print Sum of Odd Numbers in a Given Range

This C program allows the user to enter Minimum and maximum limit value. Next, the C program will calculate the sum of odd numbers between Minimum value and maximum value.

/* C Program to Print Sum of Odd Numbers in a Given Range */ #include<stdio.h> int main() { int i, Minimum, Maximum, Sum = 0; printf("\n Please Enter the Minimum, and Maximum Limit Values : \n"); scanf("%d %d", &Minimum, &Maximum); printf("\n Odd Numbers between %d and %d are : \n", Minimum, Maximum); for(i = Minimum; i <= Maximum; i++) { if ( i%2 != 0 ) { printf("%d ", i); Sum = Sum + i; } } printf("\n \n The Sum of Odd Numbers betwen %d and %d = %d", Minimum, Maximum, Sum); return 0; }

**OUTPUT**