-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0011-ReverseNumber.java
32 lines (27 loc) · 1.01 KB
/
0011-ReverseNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Yashwant Kargwal
// Don't Forget to ⭐Star and Fork.
// Problem: Write a program to reverse the digits of a given number.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Program to reverse the digits");
System.out.print("Enter Your Number : ");
int num = sc.nextInt();
int revNum = 0;
// check if number is negative
if(num < 0){
System.out.println("Please Enter Positive numbers....");
}
else{
// while loop to make reverse number
// we use while loop because we don't know the total digits in User Input
while(num != 0){
revNum = (revNum*10) + num%10; // Extract and add last digit in revNum
num /= 10; // Remove last digit form num
}
}
System.out.println("Reverse Number : "+revNum);
sc.close();
}
}