Sunday, 5 November 2017

java program that reverse the number without using loop



Write a program that reverse a number of five elements 10000 for example user enter 1234 9it display 43219
Description:
Using % operator and multiplying by 10:
Solution
import java.util.*;
public class Message {
 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
   int num,sum=0;
   System.out.println("Enter a number of five element");
   num=in.nextInt();
    sum=sum*10+num%10;
   num/=10;
  
   sum=sum*10+num%10;
   num/=10;
    sum=sum*10+num%10;
   num/=10;
    sum=sum*10+num%10;
   num/=10;
    sum=sum*10+num%10;
 
   System.out.println("The reverse is  ="+sum);
 
 }
}
Output:
                   

No comments:

Post a Comment