C Program to Find Average of n Odd and Even Numbers
In this article, we are going to write a c program to find Average of n Odd and Even Numbers.
We will make this program in the following way -:
- C Program to Find the Average of n Odd and Even Numbers Using For Loop
- C Program to Find the Average of n Odd and Even Numbers Using While Loop
- C Program to Find the Average of n Odd and Even Numbers Using Do While Loop
- C Program to Find the Average of n Odd and Even Numbers Using Function
To make this program, we will use the following concept -:
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 above.
Now without wasting time let’s start programming.
C Program to Find Average of n Odd and Even Numbers Using For Loop
Program -:
//C Program to Find the average of n odd Numbers in a Given Range (Using For Loop)
#include<stdio.h>
void main()
{
//Declaring Variable
int num1, num2, i, sum = 0,count = 0;
float avg;
//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;
count++;
}
}
avg = (float)sum/count;
printf("\nAverage of odd Numbers in Given Range : %f", avg);
}
Output -:
Enter Starting Number
1
Enter Ending Number
10
Average of odd Numbers in Given Range : 5
C Program to Find the Average of n Odd and Even Numbers Using While Loop
Program -:
//C Program to Find the average of n odd Numbers in a Given Range Using While Loop
#include<stdio.h>
void main()
{
//Declaring Variable
int num1, num2, i, sum = 0,count = 0;
float avg;
//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;
count++;
}
num1++;
}
avg = (float)sum/count;
printf("\nAverage of odd Numbers in Given Range : %f", avg);
}
Output -:
Enter Starting Number
1
Enter Ending Number
20
Average of odd Numbers in Given Range : 10.000000
C Program to Find the Average of n Odd and Even Numbers Using Do While Loop
Program -:
//C Program to Find the average of n odd Numbers in a Given Range Using Do While Loop
#include<stdio.h>
void main()
{
//Declaring Variable
int num1, num2, i, sum = 0,count = 0;
float avg;
//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;
count++;
}
num1++;
}while(num1<=num2);
avg = (float)sum/count;
printf("\nAverage of odd Numbers in Given Range : %f", avg);
}
Output -:
Enter Starting Number
1
Enter Ending Number
10
Average of odd Numbers in Given Range : 5.000000
C Program to Find the Average of n Odd and Even Numbers Using Function
Program -:
//C Program to Find the Sum of n odd Numbers in a Given Range (Using while Loop)
#include<stdio.h>
int average(int , int);
void main()
{
//Declaring Variable
int num1, num2, avg ;
//Input Number
printf("Enter Starting Number\n");
scanf("%d",&num1);
printf("Enter Ending Number\n");
scanf("%d",&num2);
avg = average(num1, num2);
printf("\Average of odd Numbers in Given Range : %d", avg);
}
int average(int num1, int num2)
{
int sum = 0, count = 0;
do
{
if(num1%2 != 0)
{
sum = sum + num1;
count++;
}
num1++;
}while(num1<=num2);
return (sum/count);
}
Output -:
Enter Starting Number
11
Enter Ending Number
20
Average of odd Numbers in Given Range : 15
Read More
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
So friends, in this article, we have write a program to calculate the Average n Odd and Even numbers in five different ways.
- C Program to Find the Average of n Odd and Even Numbers Using For Loop
- C Program to Find the Average of n Odd and Even Numbers Using While Loop
- C Program to Find the Average of n Odd and Even Numbers Using Do While Loop
- C Program to Find the Average of n Odd and Even Numbers Using Function
- C Program to Find the Average of n Odd and Even Numbers Using Recursion
If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.