Add reverse number (#2759)
This commit is contained in:
parent
5834a949a6
commit
bc6c8544b1
30
Maths/ReverseNumber.java
Normal file
30
Maths/ReverseNumber.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package Maths;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.lang.IllegalStateException;
|
||||||
|
|
||||||
|
public class ReverseNumber {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int number;
|
||||||
|
int reverse = 0;
|
||||||
|
|
||||||
|
try (Scanner sc = new Scanner(System.in)) {
|
||||||
|
System.out.println("Enter a number:");
|
||||||
|
number = sc.nextInt();
|
||||||
|
} catch (NoSuchElementException | IllegalStateException e) {
|
||||||
|
System.out.println("ERROR: Invalid input");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while(number != 0) {
|
||||||
|
int remainder = number % 10;
|
||||||
|
|
||||||
|
reverse = reverse * 10 + remainder;
|
||||||
|
number = number/10;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("The reverse of the given number is: " + reverse);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user