Q:

C Program to Find the Sum of n odd Numbers

belongs to collection: Basic C Programming Examples

0

you have to 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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Find the Sum of Three Numbers... >>
<< C Program to Find Sum of n Natural Numbers...