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.
import java.util.Scanner;
public class Frequency
{
public static void main(String[] args)
{
int p, q, count1 = 0, count2 = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in matrix:");
p = s.nextInt();
System.out.print("Enter number of columns in matrix:");
q = s.nextInt();
int a[][] = new int[p][q];
System.out.println("Enter all the elements of matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Given Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
if((a[i][j] % 2) == 0)
{
count1++;
}
else
{
count2++;
}
}
}
System.out.println("Even number frequency:"+count1);
System.out.println("Odd number frequency:"+count2);
}
}
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 Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Programming MCQs
- Apply for Java Internship
- Practice Information Technology MCQs
- Practice BCA MCQs
- Check Programming Books