Separate even and odd position numbers in an array


Solution In C

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=1;j<=size;j++)
    {
        scanf("%d",&arr[j]);
    }

   printf("\nOdd position numbers are:");
    for(i=1;i<=size;i++)
    {
        if(i%2!=0)
        {
            printf("%d ",arr[i]);
        }
    }

    printf("\nEven position numbers are:");
    for(i=1;i<=size;i++){
        if(i%2==0)
        {
            printf("%d ",arr[i]);
        }
    }

    }


Input/Output:
Enter the size of the array:6
Enter the Element of the array:
4
3
2
8
7
9

Odd position numbers are:4 2 7
Even position numbers are:3 8 9


Solution In C++

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 position numbers are:";
    for(i=0;i<size;i++)
    {
        if(i%2!=0)
        {
            cout<<arr[i]<<" ";
        }
    }

    cout<<"\nEven position numbers are:";
    for(i=0;i<size;i++){
        if(i%2==0)
        {
            cout<<arr[i]<<" ";
        }
    }
    }


Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
3
9
8
7

odd position numbers are:3 8
Even position numbers are:4 9 7

Solution In Java

Code:

import java.util.Scanner;
public class p18 {

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 position numbers are:");
    for(i=0;i<size;i++)
    {
     
        if(i%2!=0)
        {
        System.out.print(arr[i]+" ");
        }
        }
    
    System.out.println("\nEven position numbers are:");
    for(i=0;i<size;i++){
        if(i%2==0)
        {
        System.out.print(arr[i]+" ");
        }
            }

    sc.close();
    }


}


Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
3
13
17
15
Odd position numbers are:
13 15 
Even position numbers are:
3 17 

Solution In Python

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 position numbers are:")
for i in range(0,size):
    if (i % 2 != 0):
        print(arr[i],end=" ")

print("\nEven position numbers are:")
for i in range(0,size):
    if (i % 2 == 0):
        print(arr[i],end=" ")

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
7
31
39
47
52

Odd position numbers are:
31 47 
Even position numbers are:
7 39 52 

Post a Comment

0 Comments