Write a program to reverse all the digits of a given integer.
import java.util.Scanner;
class assignment1{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int n = sc.nextInt();
reverseNum(n);
sc.close();
}
public static void reverseNum(int num) {
int n = num;
int rev = 0;
while (n > 0) {
int rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
System.out.println("Reverse of " + num + " is " + rev);
}
}
OUTPUT:
Enter the number :
1234
Reverse of 1234 is 4321
Comments
Post a Comment
Please do not enter any spam link in the comment box.