Calculate average of odd and even numbers in C++
- Home
- Find elements
- Calculate average of odd and even numbers in C++
- On
- By
- 0 Comment
- Categories: Find elements
Calculate average of odd and even numbers in C++
Calculate average of odd and even numbers in C++
In this tutorial, we will discuss a concept of the C++ code to calculate the average of odd and even numbers.
In this article, we are going to learn how to calculate the Average of odd and even numbers in the C++ programming language
What is an even or odd number?
When any integer ends in 0,2,4,6,8 and it can be divided by two with the remainder of zero, it is called as an even number.
Example of even numbers – 34,-64,78,788
When any integer ends in 0,1,3,5,7,9 and it cannot be divided without a remainder, it is called as an odd number.
Example for odd numbers – 33,-69,75,785
Here, We can use a modular operator to find odd or even number in the range of numbers
if n%2==0, n is an even number – if the number is even, the remainder is zero.
if n%2==1, n is an odd number – if the number is odd, the remainder is one.
Calculate the average of odd and even using for loop
Program 1
This program allows the user to enter a value and calculate average of odd and even numbers until the entered number using “for loop”.
Program
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num, oddSum=0,evenSum=0,oddCount=0,evenCount=0; //1 double evenAvg,oddAvg; //2 cout<<"Enter the value for num: "; //3 cin>>num; for(i=1; i<=num; i++){ //4 if(i % 2==0){ //5 evenSum=evenSum+i; evenCount++; } else{ //6 oddSum=oddSum+i; oddCount++; } } evenAvg=evenSum/evenCount; //7 oddAvg=oddSum/oddCount; cout<<"Average of odd numbers are: "<<oddAvg; //8 cout<<"\n"; cout<<"Average of even numbers are: "<<evenAvg; getch(); return 0; }
When the above code is executed, it produces the following results
Enter the value for num: 60 Average of odd numbers are: 30 Average of even numbers are: 31
Method:
- To begin with , declare and initialize four variables to find sum as oddSum=0, evenSum=0, oddCount=0 and evenCount=0;
- Declare two variables for find average named as avgOdd, avgEven;
- Next, a number is received from the user as an input for variable num
- Then, using the “for loop”, elements are picked one by one from the numbers to determine oddness and evenness.
- Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by “else statement”.
- After that, the “else statement” adds to oddSum.
- Then, find the average of odd and even numbers
- Finally, the average of odd numbers and even numbers are displayed.
Calculate the average of odd and even numbers in C++ using while loop
program
This program allows the user to enter a value and calculate average of odd and even numbers until the entered value using “while loop”.
#include <iostream> #include <conio.h> using namespace std; int main() { int i,num, oddSum=0,evenSum=0,oddCount=0,evenCount=0; //1 double evenAvg,oddAvg; //2 cout<<"Enter the value for num: "; //3 cin>>num; i=1; while(i<=num){ //4 if(i % 2==0){ //5 evenSum=evenSum+i; evenCount++; } else{ //6 oddSum=oddSum+i; oddCount++; } i++; } evenAvg=evenSum/evenCount; //7 oddAvg=oddSum/oddCount; cout<<"Average of odd numbers are: "<<oddAvg; //8 cout<<"\n"; cout<<"Average of even numbers are: "<<evenAvg; getch(); return 0; }
When the above code is executed, it produces the following results
Enter the value for num: 35 Average of odd numbers are: 18 Average of even numbers are: 18
Method:
- To begin with , declare and initialize four variables to find sum as oddSum=0, evenSum=0, oddCount=0 and evenCount=0;
- Declare two variables for find average named as avgOdd, avgEven;
- Next, a number is received from the user as an input for variable num
- Then, using the “while loop“, elements are picked one by one from the numbers to determine oddness and evenness.
- Next, “if statements” are used to find a number and then if it is even, it is added to evenSum.If the number is not even, it is dealt with by “else statement”.
- After that, the “else statement” adds to oddSum.
- Then, find the average of odd and even numbers
- Finally, the average of odd numbers and even numbers are displayed.
Similar post
Cpp program to check whether a number is even or odd
Cpp program to check a number is even or odd using the function
Cpp program to separate even and odd number from an array
Cpp program to display even and odd numbers from 1 to n
Display odd and even numbers without if statements
C++ program to check odd and even using recursion
Cpp program to display even and odd numbers in the given range
Cpp program to calculate the sum of odd and even numbers
Cpp program to calculate the sum of odd and even numbers
C++ code to calculate the average of odd and even in an array
C++ code to calculate the sum of odd and even numbers in an array
Suggested for you