4 Here is code to reverse a positive integer number, that is we input 123 and th
ID: 3601128 • Letter: 4
Question
4 Here is code to reverse a positive integer number, that is we input 123 and the computer will output 321. Please check whether this code is correct; if not, please correct it import java.util Scanner: public class ReverseNumber I Reverses the digits of an integer mathematically public static void main(Stringll args) int number, lastDigit, reverse 0; Scanner scan-new Scanner(System.in) System.out.prini"Enter a positive integer: ": a -scan nextint0 do reverse (reverse 10) + a b-b9610; a-a, 10; while (ac0); System.out printin"That mumber reversed is"+reverse): 6/9Explanation / Answer
import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is: "+reversenum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.