Note: 1- An odd number is an integer which is not a multiple of two. If it is divided by two the result is a fraction.
2- The even numbers are all numbers that are multiples of 2.
public class CountEvenOdd {
public static void main(String args[]) {
int a[][] = new int[3][3];
int noOfOdd=0;
int noOfEven=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the element of Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Value of First Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if(a[i][j]%2==0)
{
noOfEven++;
}
else
{
noOfOdd++;
}
}
}
System.out.println("Tota even number "+noOfEven);
System.out.println("Tota odd number "+noOfOdd);
}
}
Output:
Enter the element of Matrix
1
2
3
4
5
6
7
8
9
Value of First Matrix is
1 2 3
4 5 6
7 8 9
Tota even number 4
Tota odd number 5
BUILD SUCCESSFUL (total time: 7 seconds)
2- The even numbers are all numbers that are multiples of 2.
public class CountEvenOdd {
public static void main(String args[]) {
int a[][] = new int[3][3];
int noOfOdd=0;
int noOfEven=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the element of Matrix ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Value of First Matrix is ");
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
if(a[i][j]%2==0)
{
noOfEven++;
}
else
{
noOfOdd++;
}
}
}
System.out.println("Tota even number "+noOfEven);
System.out.println("Tota odd number "+noOfOdd);
}
}
Output:
Enter the element of Matrix
1
2
3
4
5
6
7
8
9
Value of First Matrix is
1 2 3
4 5 6
7 8 9
Tota even number 4
Tota odd number 5
BUILD SUCCESSFUL (total time: 7 seconds)
|
No comments:
Post a Comment