In: Computer Science
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is
10 45 3 8
2 42
3 21 44
The value returned would be 5 (there are 5 two-digit numbers in the array)
public class Question2 {
public static void main(String args[]){
int arr[][] = {{10, 45, 3, 8}, {2, 42}, {3, 21, 44}};
System.out.println(“The number of two digit numbers is “+findNums(arr));
} //main
-java code
Java Code
public class Question2
{
public static int findNums(int a[][]) //function for finding
wheather the number is two digit numbers or not
{
int num=0;
for (int i = 0; i < a.length; ++i) //retriving the elements of
the array
{
for(int j = 0; j < a[i].length; ++j)
{
if (a[i][j]>=10 && a[i][j]<=99) //checking the number
is two digit numbers or not
{
num++; //counting
}
}
}
return num; //return the number of two digit
}
public static void main(String args[]){
int arr[][] = {{10, 45, 3, 8}, {2, 42}, {3, 21, 44}};
System.out.println("The number of two digit numbers is "+findNums(arr)); //function call
}
}
I hope the answer is clear and satisfactory . If you have any doubts feel free to ask in the comment section. Please give a thumbs up.
OUTPUT