JAVA   56

Frequency of Matrix

Guest on 29th April 2022 05:15:12 AM

  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pkg208w1a12a0;
  7. import java.io.*;
  8. import java.util.*;
  9. /**
  10.  *
  11.  * @author cisco
  12.  */
  13. public class Frequency_matrix
  14. {
  15.     public static void main(String args[])
  16.     {
  17.         int i,j,even = 0, odd = 0;
  18.         int a[][] = new int[3][3];
  19.         Scanner sc = new Scanner(System.in);
  20.        
  21.         System.out.println("Enter Matrix 1 elements : ");
  22.         for(i = 0; i < 3; i++)
  23.         {
  24.             for(j = 0; j < 3; j++)
  25.             {
  26.                 a[i][j] = sc.nextInt();
  27.             }
  28.             System.out.println();
  29.         }
  30.        
  31.         for(int k = 0; k < 3; k++)
  32.         {
  33.             for(int l = 0; l < 3; l++)
  34.             {
  35.                 if((a[k][l])%2 == 0)
  36.                 {
  37.                     even += 1;
  38.                 }
  39.                 else if((a[k][l])%2 != 0)
  40.                 {
  41.                     odd += 1;
  42.                 }
  43.             }
  44.         }
  45.        
  46.         System.out.println("Odd number count in matrix : " + odd);
  47.         System.out.println("Even number count in matrix : " + even);
  48.     }
  49. }
  50.  
  51. /*
  52. run:
  53. Enter Matrix 1 elements :
  54. 1 2 3
  55.  
  56. 4 5 6
  57.  
  58. 7 8 9
  59.  
  60. Odd number count in matrix : 5
  61. Even number count in matrix : 4
  62. BUILD SUCCESSFUL (total time: 8 seconds)
  63. */

Raw Paste


Login or Register to edit or fork this paste. It's free.