# Find Sum of Odd Numbers From 1 to N in C

To find the sum of odd numbers in C, first, we need to check number is odd or not.

There are many different ways to check the number is odd or not, for example:- using modulus operator (%), using division and multiplication operator, and using a bitwise operator.

Initialize a variable `sum` with value 0. If the number is odd then add it with the variable sum.

## Sum of First Odd Numbers in C from 1 to N

To solve this problem loop can be used. Below the program find the sum of odd numbers from 1 to N numbers using for loop.

``````#include<stdio.h>
int main()
{
int n, sum=0;

printf("Enter n value: ");
scanf("%d",&n);

for(int i=1; i<=n; i++)
{
if(i%2!=0) sum += i;
}

printf("Sum of odd numbers from 1 to %d is: %d", n, sum);

return 0;
}``````

Output for the different test-cases:-

Enter n value: 10
Sum of odd numbers from 1 to 10 is: 25

Enter n value: 20
Sum of odd numbers from 1 to 20 is: 100

In this program, we first check number is odd or not. There is a more simple way to find the sum of odd numbers from 1 to N.

The first odd number is 1, then the next odd number is 3, i.e (1+2). So, update expression should be i=i+2 for the initialization variable i. It will decrease the times of iteration of the for a loop.

``````#include<stdio.h>
int main()
{
nt n, sum=0;

printf("Enter n value: ");
scanf("%d",&n);

for(int i=1; i<=n; i=i+2)
{
sum += i;
}

printf("Sum of odd numbers from 1 to %d is: %d\n", n, sum);

return 0;
}``````

Output for different test-cases:-

Enter n value: 10
Sum of odd numbers from 1 to 10 is: 25

## Sum of first N odd numbers without using a loop

We can also find the sum of odd numbers in C without using a loop. For this purpose, we need to use an odd number theorem.

`Sum of first N odd numbers = N*N`

The sum of the first N odd numbers is equal to the square of the number N.
For example, the first 5 odd numbers are:- 1, 3, 5, 7, and 9
Their sum is 1+3+5+7+9 = 25 we know that 25 is the square of the number 5.

Similarly,
The sum of the first 10 odd numbers is:-
1+3+5+7+9+11+13+15+17+19 = 100, and the 100 is the square of the number 10.

``````#include<stdio.h>
int main()
{
int n, sum;

printf("Enter n value: ");
scanf("%d",&n);

sum = n*n;

printf("The sum of first %d odd numbers is: %d\n", n, sum);

return 0;
}``````

Output for the different test-cases:-

Enter n value: 10
The sum of the first 10 odd numbers is: 100

Enter n value: 20
The sum of the first 20 odd numbers is: 400

## Sum of odd numbers within a range

Sometimes we need to find the sum of odd numbers within a range i.e. from m to n. It is similar to the previous programs.

``````#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: 10 20
Sum of odd numbers from 10 to 20 is: 75

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!