Question
Write a java program that reads an integer (greater than 0 and less than 1000) from the console and flip digits of the number, using
Write a java program that reads an integer (greater than 0 and less than 1000) from the console and flip digits of the number, using the arithmetic operators / and %. The result of the flip operation should always be a three digit number. Please make sure that your program works for one, two, and three digit inputs.
I was able to right a program that reverses a number that is already declared but am having issues reversing an input.
package reverse;
class GFG
{
/* Iterative function to reverse
digits of num*/
static int reversDigits(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Driver code
public static void main (String[] args)
{
int num = 4562;
System.out.println("Reverse of no. is "
+ reversDigits(num));
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started