mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Change project structure to a Maven Java project + Refactor (#2816)
This commit is contained in:
committed by
GitHub
parent
8e533d2617
commit
9fb3364ccc
30
src/main/java/com/thealgorithms/maths/ReverseNumber.java
Normal file
30
src/main/java/com/thealgorithms/maths/ReverseNumber.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.thealgorithms.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user