In this tutorial, we will discuss how to use the C program to calculate the sum of odd and even numbers in an array.
In this article, we are going to learn how to calculate the sum 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 number in the 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.
Program 1
This program allows the user to calculate the sum 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}; int i,oddSum=0,evenSum=0; for(i=0; i<6; i++){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of even numbers are: %d",evenSum); getch(); return 0; }
When the above code is executed, it produces the following results
The sum of odd numbers are: 45 The sum of even numbers are: 60
Methodology:
Program 2
This program allows the user to choose any value for array length and input values into the array. The program will calculate the sum of odd and even numbers from the array using “for loop”.
#include <stdio.h> #include <stdlib.h> int main() { int oddSum=0,evenSum=0; int i,size; printf("Enter the size of the array: "); scanf("%d",&size); int arr[size]; printf("Enter the Array elements: "); for(i=0; i<size; i++){ scanf("%d",&arr[i]); } for(i=0; i<size; i++){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of odd numbers are: %d",evenSum); 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: 13 24 35 46 57 68 The sum of odd numbers are: 105 The sum of even numbers are: 138
Method:
Program 1
This program allows the user to calculate the sum 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}; int i,oddSum=0,evenSum=0; i=0; while(i<6){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } i++; } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of odd numbers are: %d",evenSum); getch(); return 0; }
When the above code is executed, it produces the following results
The sum of odd numbers are: 75 The sum of even numbers are: 60
Methodology:
Program 2
This program allows the user to choose any value for array length and input values into the array. The program will calculate the sum of odd and even numbers from the array using “while loop”.
#include <stdio.h> #include <stdlib.h> int main() { int oddSum=0,evenSum=0; int i,size; printf("Enter the size of the array: "); scanf("%d",&size); int arr[size]; printf("Enter the Array elements: "); i=0; while(i<size){ scanf("%d",&arr[i]); i++; } i=0; while(i<size){ if(arr[i]%2==0){ evenSum=evenSum+arr[i]; } else{ oddSum=oddSum+arr[i]; } i++; } printf("The sum of odd numbers are: %d",oddSum); printf("\nThe sum of even numbers are: %d",evenSum); getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array: 6 Enter the array elemts: 10 11 12 13 14 15 The sum of the odd numbers are: 39 The sum of the even numbers are: 36
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 function
C program to separate Odd and Even numbers from an array
C program to Electricity bill calculation using function
C program to display all even and odd numbers from 1 to n
C program display odd and even numbers without if statements
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,…