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
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 numbers from 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.
Program 1
This program allows the user to enter a value and calculate the average of odd and even numbers until the entered value using “for loop“.
#include <stdio.h> #include <stdlib.h> int main() { int i,num, oddSum=0,evenSum=0,oddCount=0,evenCount=0; //1 double evenAvg, oddAvg; //2 printf("Enter the value for num: "); //3 scanf("%d",&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; printf("Average of odd numbers are %.2f: \n",oddAvg); //8 printf("Average of even numbers are %.2f: ",evenAvg); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the value for num: 25 Average of odd numbers are: 13.00 Average of even numbers are: 13.00
Method:
Program 1
This program allows the user to enter a value and calculate the average of odd and even numbers until the entered number using “while loop”.
#include <stdio.h> #include <stdlib.h> int main() { int i,num, oddSum=0,evenSum=0,oddCount=0,evenCount=0; //1 double evenAvg, oddAvg; //2 printf("Enter the value for num: "); //3 scanf("%d",&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; printf("Average of odd numbers are %.2f: \n",oddAvg); //8 printf("Average of even numbers are %.2f: ",evenAvg); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the value for num: 30 Average of odd numbers are: 15.00 Average of even numbers are: 16.00
Method:
Similar post
C program to check whether a number is even or odd
C program to check a number is even or odd using the function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using the function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
C program to check odd and even numbers using recursion
C program to display even and odd number in the given range
C program to calculate the sum of odd and even numbers
C code to calculate the average of odd and even in an array
C program to calculate the sum of odd and even of an array
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…