C program to separate Odd and Even numbers from an array
- Home
- Check value
- C program to separate Odd and Even numbers from an array
- On
- By
- 1 Comment
- Categories: Check value, Find elements
C program to separate Odd and Even numbers from an array
C program to separate Odd and Even numbers from an array
In this tutorial, we will discuss the C program to separate Odd and Even numbers from an array
In this post, we are going to learn how to separate the even and odd numbers from the given array
First, we must understand how to identify odd numbers or even numbers
What is Even and Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers
In my previous post, I have explained the various approaches to check whether the number is even or odd using C language
There are three examples given below
The procedure of this program
- initially, the user will enter the size of the array and store in the variable size to define the array size
- And declare an integer type array for stored elements
- Then, the user enters all the elements of the array according to the array size
- After that, use a for loop for iterating elements of the array
- finally, the following two for loops filter even and odd number using modular operator from the array
Program 1
Separate even and odd numbers using the for loop
The program allows the user to enter the size of the array and the array elements.
Then, it will separate the even and odd numbers from this array using for loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int arr[100],i,num; printf("Enter size of the array\n"); scanf("%d",&num);//reads input from user for array size printf("Enter the elements of the array\n"); for(i=0; i<num; i++){ scanf("%d",&arr[i]);//reads input from user for array elements } printf("\nEven numbers of the array are \n"); for(i=0; i<num; i++){ if(arr[i]%2==0){ printf("%d \t",arr[i]); } } printf("\nOdd numbers of the array are \n"); for(i=0; i<=num; i++){ if (arr[i]%2==1){ printf("%d \t",arr[i]); } } getch(); return 0; }
When the above code is executed, it produces the following results
Enter size of the array 8 Enter the elements of the array 23 34 45 56 67 78 89 90 Even numbers of the array are 34 56 78 90 Odd numbers of the array are 23 45 67 89
Program 2
Separate even and odd numbers using the while loop
The program allows the user to enter the size of the array and the array elements.
Then, it will separate the even and odd numbers from this array using while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int arr[100],i,num; printf("Enter size of the array \n"); scanf("%d",&num);//reads input from user for array size printf("Enter the elements of the array\n"); i=0; while(i<num){ scanf("%d",&arr[i]);//reads input from user for array elements i++; } printf("\nEven numbers of the array are \n"); i=0; while(i<num){ if(arr[i]%2==0){ printf("%d \t",arr[i]); } i++; } printf("\nOdd numbers of the array are \n"); i=0; while(i<=num){ if (arr[i]%2==1){ printf("%d \t",arr[i]); } i++; } getch(); return 0; }
When the above code is executed, it produces the following results
Enter size of the array 8 Enter the elements of an array 24 35 46 57 68 79 80 98 Even number of an array are 24 46 68 80 98 Odd numbers of an array are 35 57 79
Program 3
Separate even and odd numbers using the do-while loop
The program allows the user to enter the size of the array and the array elements.
Then, it will separate the even and odd numbers from this array using the do-while loop in C language
#include <stdio.h> #include <stdlib.h> int main() { int arr[100],i,num; printf("Enter size of the array\n"); scanf("%d",&num);//reads input from user for array size printf("Enter the elements of the array\n"); i=0; do{ scanf("%d",&arr[i]);//reads input from user for array elements i++; }while(i<num); printf("\nEven numbers of the array are \n"); i=0; do{ if(arr[i]%2==0){ printf("%d \t",arr[i]); } i++; }while(i<num); printf("\nOdd numbers of the array are \n"); i=0; do{ if (arr[i]%2==1){ printf("%d \t",arr[i]); } i++; }while(i<=num); getch(); return 0; }
When the above code is executed, it produces the following results
Enter size of the array 8 Enter the elements of an array 98 87 76 65 54 43 32 21 Even number of an array are 98 76 54 32 Odd numbers of an array are 87 65 43 21
Similar post
Java code to compute even and odd numbers of an array
C code to compute even and odd numbers of an array
Display even and odd numbers without if statement in C++
Display even and odd numbers without if statement in Java
Display even and odd numbers without if statement in C
C program to find the largest among three numbers using function
Python code to find the largest of three numbers using the function
C++ code to find the largest of three numbers using the function
Java code to find the largest of three numbers
C code to find the largest of three numbers
C++ code to find the largest of three numbers
Python code to find the largest of three numbers
Java program to find the middle of three number using Method
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Python code to find the middle of three number using the function
Java code to find middle of three numbers
C code to find middle of three numbers
C++ code to find middle of three numbers
Python code to find middle of three numbers
Java code to find largest of three numbers in an array
Java code to find smallest of three numbers in an array
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Suggested for you
Nested if statement in C++ language
The operator in Python language
If statement in Python language
Data types and Variable in Python
Datatype and variables in Java programming language
If statements in Java language
1 Comment
CodePopular August 4, 2019 at 10:13 am
Download Free HTML Template
http://codepopular.com