C Program to Find the Sum of n odd Numbers

In this article, we are going to write a c program to find the Sum of n odd numbers

We will make this program in the following way -:

  • C Program to Find the Sum of n odd Numbers in a Given Range (Using For Loop)
  • C Program to Find the Sum of n odd Numbers Using While Loop
  • C Program to Find the Sum of n odd Numbers Using Do While Loop
  • C Program to Find the Sum of n odd Numbers Using Function

To make this program, we will use the following concept given below. If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given below.

Now without wasting time let’s start programming.

C Program to Find the Sum of n odd Numbers

C Program to Find the Sum of n odd Numbers in a Given Range (Using For Loop)

Algorithm

  • Program Start
  • Variable Declaration (num1,num2,i,sum)
  • Input Numbers
  • Calculating Sum
  • Displaying the Sum of n Odd Numbers in a Given Range
  • Program End

Program

//C Program to Find the Sum of n odd Numbers in a Given Range (Using For Loop)

#include<stdio.h>
void main()
{
 //Declaring Variable
  int num1, num2, i, sum = 0 ;

//Input Number
 printf("Enter Starting Number\n");
 scanf("%d",&num1);

 printf("Enter Ending Number\n");
  scanf("%d",&num2);

 for(i=num1;i<=num2;i++)
  {   if(i%2==0)
      {
        continue ;
      }
      else
      {
        sum = sum + i;
      }
  }
  printf("\nSum of odd Numbers in Given Range : %d", sum);

}

Output

Enter Starting Number
1
Enter Ending Numeber
5
Sum of odd Numbers in Given Range : 9

C Program to Find the Sum of n odd Numbers Using While Loop

Program

//C Program to Find the Sum of n odd Numbers in a Given Range Using while Loop

#include<stdio.h>
int main()
{
 //Declaring Variable
  int num1, num2, sum = 0 ;

//Input Number
 printf("Enter Starting Number\n");
 scanf("%d",&num1);

 printf("Enter Ending Number\n");
  scanf("%d",&num2);

 while(num1<=num2)
  {   if(num1%2 != 0)
        {
            sum = sum + num1;
        }
        num1++;
  }
  printf("\nSum of odd Numbers in Given Range : %d", sum);

return 0;
}

Output

Enter Starting Number
10
Enter Ending Numeber
20
Sum of odd Numbers in Given Range : 75

C Program to Find the Sum of n odd Numbers Using Do While Loop

Program

//C Program to Find the Sum of n odd Numbers in a Given Range Using do while Loop

#include<stdio.h>
int main()
{
 //Declaring Variable
  int num1, num2, sum = 0 ;

//Input Number
 printf("Enter Starting Number\n");
 scanf("%d",&num1);

 printf("Enter Ending Number\n");
  scanf("%d",&num2);

 do
  {   if(num1%2 != 0)
        {
            sum = sum + num1;
        }
        num1++;
  }while(num1<=num2);
  printf("\nSum of odd Numbers in Given Range : %d", sum);


return 0;
}

Output

Enter Starting Number
11
Enter Ending Numeber
20
Sum of odd Numbers in Given Range : 75

C Program to Find the Sum of n odd Numbers Using Function

To Calculate the sum n odd numbers through a function, first you need to know about the function. You can read about the function from the link given below.

Algorithm

  • Program Start
  • Declaring variables
  • Input Number
  • Calling Function to calculate Sum of n odd Numbers
  • Print the Sum of n odd Numbers
  • Program End

Program

//C Program to Find the Sum of n odd Numbers in a Given Range Using Function 

#include<stdio.h>
int sum(int , int);
void main()
{
 //Declaring Variable
  int num1, num2, s ;

//Input Number
 printf("Enter Starting Number\n");
 scanf("%d",&num1);

 printf("Enter Ending Number\n");
  scanf("%d",&num2);

  s = sum(num1, num2);

  printf("\nSum of odd Numbers in Given Range : %d", s);

}

int sum(int num1, int num2)
{
     int sum = 0;
     do
   {
    if(num1%2 != 0)
    {
       sum = sum + num1;
    }
    num1++;
  }while(num1<=num2);
   return sum;
}

Output

Enter Starting Number
1
Enter Ending Numeber
25
Sum of odd Numbers in Given Range : 169

Conclusion

So friends, in this article, we have written a c program to calculate the sum n natural numbers in five different ways.

  • C Program to Find the Sum of n odd Numbers in a Given Range (Using For Loop)
  • C Program to Find the Sum of n odd Numbers Using While Loop
  • C Program to Find the Sum of n odd Numbers Using Do While Loop
  • C Program to Find the Sum of n odd Numbers Using Function

If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging

Leave a Comment