Find Sum of Odd Numbers From 1 to N in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

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 do you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

Your email address will not be published. Required fields are marked *