Write a class CountOdd - to count the odd and even numbers using control structures and arrays

0

Assignment: Module 03- Part 01

Write a class CountOdd -  to count the odd and even numbers using control structures and arrays
Assignment: Module 03- Part 01

06. Write a class CountOdd -  to count the odd and even numbers using control structures and arrays. 

Input: 

Enter the array size

3, 12, 14, 17

Output: 

No of even number is : 2 

No of odd number is : 1

import java.util.Scanner;

class CountOdd {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the array size: ");
    int size = sc.nextInt();
    int[] numbers = new int[size];

    for (int i = 0; i < size; i++) {
      System.out.print("Enter number " + (i + 1) + ": ");
      numbers[i] = sc.nextInt();
    }

    int evenCount = 0;
    int oddCount = 0;

    for (int number : numbers) {
      if (number % 2 == 0) {
        evenCount++;
      } else {
        oddCount++;
      }
    }

    System.out.println("No of even numbers is: " + evenCount);
    System.out.println("No of odd numbers is: " + oddCount);
  }
}
 

This code uses a 'Scanner' object to read the size of the array and the elements of the array from the user. It then initializes two variables 'evenCount' and 'oddCount' to keep track of the number of even and odd numbers in the array. It uses a for-each loop to iterate through the array and a simple if-else block to check whether each number is even or odd and increments the respective count variable. Finally, it prints the number of even and odd numbers in the array.


👉Write a program to find the Armstrong number using control structures.👈

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
✨ Updates