Reverse A Number In Java – We have discussed the various methods to reverse a number in Java program. To each and every program, compiler is added to execute the program. Along with it, sample outputs are also given citing various examples. The methods are:
- Using While Loop
- Using Static Method
- Using Function
- Using Recursion
Using While Loop
1) Entered value will be assigned to n and res=0.
2) While loop iterates until n!=0, if n=0 then the loop terminated and prints the res value.
- example n=153, the condition is true ,a=3, res=(0*10)+3=3, n=15
- n=15 condition true, a=5,res=30+5=35,n=1
- n=1,condition true, a=1, res=35*10+1=351,n=0, so while loop terminated and prints reverse number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class Rev { public static void main(String[] arg) { int a,res=0,n; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextInt(); while(n!=0) { a=n%10; res=(res*10)+a; n=n/10; } System.out.println("reverse of a number is "+res); } } |
Output:
1 2 3 |
Enter a number 153 reverse of a number is 351 |
Using Static Method
1) Here we have a static method reverse(int num), which calculates the reverse number.
2) The reverse method is called at the main method then reverse method executed and returns the reverse number, then prints the reverse number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Scanner; class Rev { public static void main(String[] arg) { Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); int x=sc.nextInt(); int r=reverse(x); System.out.println("Reverse of a number is = "+r); } static int reverse(int num) { int rem,res=0; while(num!=0) { rem=num%10; res=(res*10)+rem; num=num/10; } return res; } } |
Output:
1 2 3 |
Enter a number 159 Reverse of a number is = 951 |
Reverse A Number In Java – Using Function
1) We are calculating the reverse of a number using for loop.
2) For loop iterates until n!=0, here the structure of for loop doesn’t contains the initialization ,increment / decrement.
3) For loop repeats these steps a=n%10; res=(res*10)+a; n=n/10; until the condition is false. if n=0 then it prints reverse of the number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; class Rev { public static void main(String[] arg) { int a,res=0,n; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextInt(); for( ;n!=0; ) { a=n%10; res=(res*10)+a; n=n/10; } System.out.println("Reverse of a number is = "+res); } } |
Output:
1 2 3 |
Enter a number 909 Reverse of a number is = 909 |
Using Recursion
1) In this program reverse(int num) is recursive, it calls itself until the condition is false.
2) Using Rev class object r, call the method reverse(x ) as r.reverse(x), then reverse(x) method starts the execution and calls itself as reverse(num) until num!=0.
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 |
import java.util.Scanner; class Rev { int res=0; void reverse(int num) { int rem; if(num!=0) { rem=num%10; res=(res*10)+rem; num=num/10; reverse(num); } else System.out.println("reverse of a number is "+res); } public static void main(String[] arg) { Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); int x=sc.nextInt(); Rev r=new Rev(); r.reverse(x); } } |
Output:
1 2 3 |
Enter a number 9582 reverse of a number is 2859 |
If you have any doubts or suggestions about Reverse A Number In Java program do leave a comment here.
More Programs: