Computer Science, asked by hemanth21920005, 4 months ago

Write a program to accept a mumber and check if it is a three-digit mamber or not. it is a three-digit mumber, print the maximum digit of the given number using Math class method otherwise print it is not a three-digit number. For example, if the user enters 2345, the output should be "It is not a three-digit number". Sample Input Enter a number 455 Sample Output The maximum digit in the given number is: 8​

Attachments:

Answers

Answered by anindyaadhikari13
1

Question:-

Write a program to accept a number and check if it is a three-digit number or not. it is a three-digit number, print the maximum digit of the given number using Math class method otherwise print it is not a three-digit number.

Code:-

class x

{

public static void main(int num)

{

if(num/100<=9)

{

System.out.println("Number is a three digit number. ");

int max=0;

int a[]=new int[3], i=0;

for(;num!=0;num/=10, i++)

a[i]=num%10;

max=Math.max(a[0], Math.max(a[1]), Math.max(a[2]));

System.out.println("Largest digit is: "+max);

}

else

System.out.println("Number is not a three digit number.");

}

}

Explanation:-

Consider a number, say num=999

num/100=9 which is less than or equal to 9. So, it is a three digit number.

Consider, num=544, num/100=5 and 5<=9 is true. So, it is also a three digit number.

Again, if num=1000, num/100=10 which is greater than 9, so it is not a three digit number. In this way, we have checked whether it is a three digit number or not. Now, we will assign each digit of the number in an array and from that, we will calculate the largest digit using Math.max() function.

Similar questions