In Java : Randomly generate an integer number in range [10, 99].
Write a program to check if
the digit at 10’s position is less than the digit at 0’s position.
If yes, swap these 2
digits. Display original number and new number. For example, if the
number
generated is 53, after process, the new number will be 35. If
original number is 12,
no process needed. If the number is 50, the new number will be
5.
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
public class Main
{
public static void main(String[] args)
{
//variables declaration.
int low=10,high=100,new_num=0;
int random_num= (int) (Math.random() * (high - low)) +
low; //random number generator.
//separate two digits.
int zeros_pos=random_num/10;
int tenth_pos=random_num%10;
if(tenth_pos<=zeros_pos) //condition compare two
digits.
{
//calculate new number.
new_num=(new_num*10)+tenth_pos;
new_num=(new_num*10)+zeros_pos;
//print new number.
System.out.println("original number:
"+random_num);
System.out.println("new number: "+new_num);
}
else
System.out.println("original number:
"+random_num);
}
}
OUTPUT:
SCREENSHOT OF THE CODE:
Get Answers For Free
Most questions answered within 1 hours.