Showing posts with label even and odd numbers in an array. Show all posts
Showing posts with label even and odd numbers in an array. Show all posts

Tuesday, 13 January 2015

Cpp Program to find all the even and odd numbers in an array


This is a Cpp Program which finds all the even and odd numbers in an array.

Here we check all the array elements for even condition that number % 2 is 0 or not , now we print all the numbers satisfies this condition as  even numbers and the other numbers as odd numbers.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int n,i;
    cout << "\nEnter the no of Elements: \t";
    cin >> n;
    int arr[n];
    for(i=0;i<n;i++)
    {
        cout << "\nEnter Element " <<  i+1 << ": \t";
        cin >> arr[i];
    }
    cout  << "\nThe EVEN Numbers are : \t";
    for(i=0;i<n;i++){
        if(arr[i]%2 == 0)

        cout << "\t" << arr[i];
    }
    cout  << "\nThe ODD Numbers are : \t";
    for(i=0;i<n;i++){
        if(arr[i]%2 != 0)

        cout << "\t" << arr[i];
    }
    return 0;
}

OUTPUT : 
Cpp - Even or Odd elements in an array

Thursday, 8 January 2015

Java Program to find all the Even and Odd numbers in an array


This is a Java Program which finds the even and odd numbers in an array.

Here all the elements in the array are checked for even and odd condition , when the condition satisfies it prints out the result.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class EvenOddArray {
 public static void main(String[] args) {
  int i, n;
  Scanner scan = new Scanner(System.in);
  System.out.print("How many elements ?\t");
  n = scan.nextInt();
  int a[] = new int[n];
  for (i = 0; i < n; i++) {
   System.out.print("Enter number " + (i + 1));
   a[i] = scan.nextInt();
  }
  scan.close();
  System.out.print("\nThe Even Numbers are : \t");
  for (i = 0; i < n; i++) {

   if (a[i] % 2 == 0)
    System.out.print(a[i] + "\t");

  }
  System.out.print("\nThe Odd Numbers are : \t");
  for (i = 0; i < n; i++) {

   if (a[i] % 2 != 0)
    System.out.print(a[i] + "\t");

  }
 }
}

OUTPUT :
Java - Even and Odd numbers in an array

C Program to find all the Even and Odd numbers in an array


This is a C Program which finds the even and odd numbers in an array.

Here all the elements in the array are checked for even and odd condition , when the condition satisfies it prints out the result

PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n;
    printf("How many elements ?\t");     
    scanf("%d",&n); 
    int a[n];
    for(i=0;i<n;i++)                    
    {
        printf("Enter number %d \t",i+1);
        scanf("%d",&a[i]);
    }
    printf("\nThe Even Numbers are : \t");   
    for(i=0;i<n;i++)
    {

        if(a[i]%2 == 0)
            printf("%d \t",a[i]);

    }
    printf("\nThe Odd Numbers are : \t");  
    for(i=0;i<n;i++)
    {

        if(a[i]%2 != 0)
            printf("%d \t",a[i]);

    }
    return 0;
}

OUTPUT :
C - Even and Odd Elements in an Array
Related Programs: