Java Program to Find the Frequency of Odd & Even Numbers in the given Matrix

«
»
This is a Java Program to Find the Frequency of Odd & Even Numbers in the given Matrix.

Enter the elements of array as input. First we check the elements are either even or odd and hence increment the corresponding variable to get result.

Here is the source code of the Java Program to Find the Frequency of Odd & Even Numbers in the given Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. public class Frequency
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int p, q, count1 = 0, count2 = 0;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter number of rows in matrix:");
  9.         p = s.nextInt();
  10.         System.out.print("Enter number of columns in matrix:");
  11.         q = s.nextInt();
  12.         int a[][] = new int[p][q];
  13.         System.out.println("Enter all the elements of matrix:");
  14.         for (int i = 0; i < p; i++) 
  15.         {
  16.             for (int j = 0; j < q; j++) 
  17.             {
  18.                 a[i][j] = s.nextInt();
  19.             }
  20.         }
  21.         System.out.println("Given Matrix:");
  22.         for (int i = 0; i < p; i++) 
  23.         {
  24.             for (int j = 0; j < q; j++) 
  25.             {
  26.                 System.out.print(a[i][j] + " ");
  27.             }
  28.             System.out.println("");
  29.         }
  30.         for (int i = 0; i < p; i++) 
  31.         {
  32.             for (int j = 0; j < q; j++) 
  33.             {
  34.                 if((a[i][j] % 2) == 0)
  35.                 {
  36.                     count1++;
  37.                 }
  38.                 else
  39.                 {
  40.                     count2++;
  41.                 }
  42.             }
  43.         }
  44.         System.out.println("Even number frequency:"+count1);
  45.         System.out.println("Odd number frequency:"+count2);
  46.     }
  47. }

Output:

$ javac Frequency.java
$ java Frequency
 
Enter number of rows in matrix:3
Enter number of columns in matrix:3
Enter all the elements of matrix:
1
2
3
4
5
6
7
8
9
Given Matrix:
1 2 3 
4 5 6 
7 8 9 
Even number frequency:4
Odd number frequency:5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter