C code to Calculate average of odd and even in an array
- Home
- Check value
- C code to Calculate average of odd and even in an array
- On
- By
- 0 Comment
- Categories: Check value, Find elements
C code to Calculate average of odd and even in an array
C code to Calculate average of odd and even in an array
In this tutorial, we will discuss a concept of the C code to calculate the average of odd and even numbers in an array.
In this article, we are going to learn how to calculate the Average of odd and even numbers of an array 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 an array
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.
C code to Calculate the average of odd and even numbers using for loop
Program 1
This program allows the user to calculate the average of odd and even numbers in the given array using “for loop”.
#include <stdio.h> #include <stdlib.h> int main() { int arr[6]={5,10,15,20,25,30}; // 1 int i,oddSum=0,evenSum=0, oddCount=0,evenCount=0; // 2 double avgOdd,avgEven;//3 for(i=0; i<6; i++){ //4 if(arr[i]%2==0){ evenSum=evenSum+arr[i]; //5 evenCount++; } else{ oddSum=oddSum+arr[i]; //6 oddCount++; } } avgOdd=oddSum/oddCount; //7 avgEven=evenSum/evenCount; printf("The average of odd numbers are: %.2f",avgOdd); //8 printf("\nThe average of even numbers are: %.2f",avgEven); getch(); return 0; }
When the above code is executed, it produces the following results
The average of odd numbers are: 15.00 The average of even numbers are: 20.00
Method:
- First, define an array with elements.
- Next, declare and initialize four variables to find sum as oddSum=0, evenSum=0, oddCount=0, evenCount=0;
- Then, declare variables to find average as avgOdd, avgEven;
- Thereafter, use the “for loop” to take the elements one by one from the given array.
- The “if statement” finds a number and then if the number is even, it is added to evenSum. If the number is not even, it is dealt with by “else statement”.
- The “else statement” finds odd numbers and adds to oddSum.
- Then, find the average of odd and even numbers
- Finally, the average of odd numbers and even numbers are displayed.
Program 2
This program allows the user to choose any value for array length(Number of array elements) and input values into the array. The program will calculate the Average of odd and even numbers from the list using “for loop”.
#include <stdio.h> #include <stdlib.h> int main() { int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0,size; // 1 double avgOdd,avgEven;//2 printf("Enter the size of the array: "); //3 scanf("%d",&size); int arr[size]; //4 printf("Enter the Array elements: ");//5 for(i=0; i<size; i++){//6 scanf("%d",&arr[i]); } for(i=0; i<size; i++){//7 if(arr[i]%2==0){//8 evenSum=evenSum+arr[i]; evenCount++; } else{ //8 oddSum=oddSum+arr[i]; oddCount++; } } avgOdd=oddSum/oddCount; //9 avgEven=evenSum/evenCount; printf("The average of odd numbers are: %.2f",avgOdd); //10 printf("\nThe average of even numbers are: %.2f",avgEven); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array: 6 Enter the array elements: 100 105 110 115 120 125 The average of odd numbers are: 115.00 The average of even numbers are: 110.00
Method:
- To begin with , declare and initialize two variables to find sum as oddSum=0, evenSum=0, oddCount=0, evenCount=0;
- Declare two variables for find average named as avgOdd, avgEven;
- Next, receives input from the user regarding the array length.
- Then, defines an array without including the elements.
- Afterwards, the elements for an array is received from the user.
- Then, using the “for loop”, the elements are added one by one to the array.
- Then, using the second “for loop”, elements are picked one by one from the array 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.
C code to Calculate the average of odd and even numbers using while loop
Program 1
This program allows the user to calculate the average of odd and even numbers in the given array using “while loop”.
#include <stdio.h> #include <stdlib.h> int main() { int arr[6]={10,15,20,25,30,35}; //1 int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0; // 2 double avgOdd,avgEven; //3 i=0; while(i<6){ //4 if(arr[i]%2==0){ evenSum=evenSum+arr[i]; //5 evenCount++; } else{ oddSum=oddSum+arr[i]; //6 oddCount++; } i++; } avgOdd=oddSum/oddCount; //7 avgEven=evenSum/evenCount; printf("The average of odd numbers are: %.2f",avgOdd); //8 printf("\nThe average of even numbers are: %.2f",avgEven); getch(); return 0; }
When the above code is executed, it produces the following results
The average of odd numbers are: 20.00 The average of even numbers are: 25.00
Method:
- First, define an array with elements.
- Next, declare and initialize two variables to find sum as oddSum=0, evenSum=0, oddCount=0, evenCount=0;
- Then, declare and initialize two variables to find average as avgOdd, avgEven;
- Thereafter, use the “while loop” to take the elements one by one from the array.
- The “if statement” finds a number and then if the number is even, it is added to evenSum. If the number is not even, it is dealt with by “else statement”.
- The “else statement” finds odd numbers and adds to oddSum.
- Then, find the average of odd and even numbers
- Finally, the average of odd numbers and even numbers are displayed.
Program 2
This program allows the user to choose any value for array length(Number of array elements) and input values into the array. The program will calculate the Average of odd and even numbers from the list using “for loop”.
#include <stdio.h> #include <stdlib.h> int main() { int i,oddSum=0,evenSum=0,oddCount=0,evenCount=0,size; // 1 double avgOdd,avgEven;//2 int arr[size];//3 printf("Enter the size of the array: ");//4 scanf("%d",&size); printf("Enter the Array elements: ");//5 i=0; while(i<size){//6 scanf("%d",&arr[i]); i++; } i=0; while(i<size){//7 if(arr[i]%2==0){//8 evenSum=evenSum+arr[i]; evenCount++; } else{ oddSum=oddSum+arr[i];//9 oddCount++; } i++; } avgOdd=oddSum/oddCount; //10 avgEven=evenSum/evenCount; printf("The average of odd numbers are: %.2f",avgOdd); //11 printf("\nThe average of even numbers are: %.2f",avgEven); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array: 5 Enter the array elements: 21 23 32 35 44 The average of the odd numbers are: 26.00 The average of the even numbers are: 38.00
Method:
- To begin with , declares and initializes three variables: oddSum=0, evenSum=0 and size.
- Then, declare and initialize two variables to find average as avgOdd, avgEven;
- Next, defines an array without including the elements.
- Thereafter, receives input from the user regarding the array length.
- Afterwards, the elements for an array is received from the user.
- Then, using the “while loop”, the elements are added one by one to the array.
- Then, using the second “while loop”, elements are picked one by one from the array 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
C program to find 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 calculate the sum of odd and even numbers
C program to find whether a number is even or odd
C++ program to find a number is even or odd using the function
C++ program to separate Odd and Even numbers from an array
C++ program to display all even and odd numbers from 1 to n
C++ program calculate Average of odd and even numbers in an array
C++ program to calculate the sum of odd and even numbers
C++ program to find whether a number is even or odd
Java program to find a number is even or odd using the method
Java program to separate Odd and Even numbers from an array
Java program to display all even and odd numbers from 1 to n
Java program display odd and even numbers without if statements
Java program to calculate the sum of odd and even numbers
Java program to find whether a number is even or odd
Suggested for you
Data type and variable in Java