Problem statement:- Program to Separate even and odd numbers in an array.
Data requirement:-
Input Data:- arr,size
Output Data:-arr
Output Data:-arr
Additional Data:- i, j
Program in C
Here is the source code of the C Program to Separate even and odd numbers in an array.
Code:
#include<stdio.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j,i;
printf("Enter the Element of the array:\n");
for(j=0;j<size;j++)
{
scanf("%d",&arr[j]);
}
printf("\nodd numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]%2!=0)
{
printf("%d ",arr[i]);
}
}
printf("\nEven numbers are:");
for(i=0;i<size;i++){
if(arr[i]%2==0)
{
printf("%d ",arr[i]);
}
}
}
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],j,i;
printf("Enter the Element of the array:\n");
for(j=0;j<size;j++)
{
scanf("%d",&arr[j]);
}
printf("\nodd numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]%2!=0)
{
printf("%d ",arr[i]);
}
}
printf("\nEven numbers are:");
for(i=0;i<size;i++){
if(arr[i]%2==0)
{
printf("%d ",arr[i]);
}
}
}
Input/Output:
Enter the size of the array:5
Enter the Element of the array:
7
8
4
3
2
odd numbers are:7 3
Even numbers are:8 4 2
Enter the Element of the array:
7
8
4
3
2
odd numbers are:7 3
Even numbers are:8 4 2
Program in C++
Here is the source code of the C++ Program to Separate even and odd numbers in an array.
Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],j=0,i,temp,k,temp2;
cout<<"Enter the Element of the array:\n";
for(j=0;j<size;j++)
{
cin>>arr[j];
}
cout<<"\nodd numbers are:";
for(i=0;i<size;i++)
{
if(arr[i]%2!=0)
{
cout<<arr[i]<<" ";
}
}
cout<<"\nEven numbers are:";
for(i=0;i<size;i++){
if(arr[i]%2==0)
{
cout<<arr[i]<<" ";
}
}
}
Input/Output:using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],j=0,i,temp,k,temp2;
cout<<"Enter the Element of the array:\n";
for(j=0;j<size;j++)
{
cin>>arr[j];
}
cout<<"\nodd numbers are:";
for(i=0;i<size;i++)
{
if(arr[i]%2!=0)
{
cout<<arr[i]<<" ";
}
}
cout<<"\nEven numbers are:";
for(i=0;i<size;i++){
if(arr[i]%2==0)
{
cout<<arr[i]<<" ";
}
}
}
Enter the size of the array:6
Enter the Element of the array:
7
9
3
4
6
8
odd numbers are:7 9 3
Even numbers are:4 6 8
Enter the Element of the array:
7
9
3
4
6
8
odd numbers are:7 9 3
Even numbers are:4 6 8
Program in Java
Here is the source code of the Java Program to Separate even and odd numbers in an array.
Code:
import java.util.Scanner;
public class p15 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
System.out.println("Odd numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]%2!=0)
{
System.out.print(arr[i]+" ");
}
}
System.out.println("\nEven numbers are:");
for(i=0;i<size;i++){
if(arr[i]%2==0)
{
System.out.print(arr[i]+" ");
}
}
sc.close();
}
}
Input/Output:public class p15 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
System.out.println("Odd numbers are:");
for(i=0;i<size;i++)
{
if(arr[i]%2!=0)
{
System.out.print(arr[i]+" ");
}
}
System.out.println("\nEven numbers are:");
for(i=0;i<size;i++){
if(arr[i]%2==0)
{
System.out.print(arr[i]+" ");
}
}
sc.close();
}
}
Enter the size of the array:
5
Enter the Element of the array:
4
2
3
1
9
Odd numbers are:
3 1 9
Even numbers are:
4 2
5
Enter the Element of the array:
4
2
3
1
9
Odd numbers are:
3 1 9
Even numbers are:
4 2
Program in Python
Here is the source code of the Python Program to Separate even and odd numbers in an array.
Code:
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("\nOdd numbers are:")
for i in range(0,size):
if (arr[i] % 2 != 0):
print(arr[i],end=" ")
print("\nEven numbers are:")
for i in range(0,size):
if (arr[i] % 2 == 0):
print(arr[i],end=" ")
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("\nOdd numbers are:")
for i in range(0,size):
if (arr[i] % 2 != 0):
print(arr[i],end=" ")
print("\nEven numbers are:")
for i in range(0,size):
if (arr[i] % 2 == 0):
print(arr[i],end=" ")
Input/Output:
Enter the size of the array: 7
Enter the Element of the array:
5
66
88
72
65
42
65
Odd numbers are:
5 65 65
Even numbers are:
66 88 72 42
Enter the Element of the array:
5
66
88
72
65
42
65
Odd numbers are:
5 65 65
Even numbers are:
66 88 72 42
0 Comments
Please do not Enter any spam link in the comment box