Add reverse number (#2759)

This commit is contained in:
Aitor Fidalgo Sánchez 2021-10-30 07:25:54 +02:00 committed by GitHub
parent 5834a949a6
commit bc6c8544b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

30
Maths/ReverseNumber.java Normal file
View 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);
}
}