Java : Problem - 06

  

Problem: -

  1. Write a program to reverse a given number.

Solution: -

1. Write a program to reverse a given number.

import java.util.Scanner;

class reverse {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);

System.out.print("Enter any number: ");
int num = scan.nextInt();

int reverse = 0;

while (num != 0) {
reverse = reverse * 10;
reverse = reverse + num%10;
num = num/10;
}

System.out.println("Reversed Number: " +reverse);
}
}

Output: -

Mj Mausam

Post a Comment

Previous Post Next Post