Java Tutorial
This java program is used to compute the reverse number for the given number as user input and uses for loop here.
import java.util.Scanner; public class Reverse { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Reverse Number"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int remainder, reverse=0; for (int temp=number; temp!=0; temp=temp/10){ remainder = temp % 10; reverse = (reverse*10)+remainder; } System.out.println("Reverse of Number '"+number+"'': "+reverse);; } }Output:
D:\Java_Programs>javac Reverse.java D:\Java_Programs>java Reverse Reverse Number ----------------------- Enter Number: 546 Reverse of Number '546'': 645
This java program is used to compute the reverse number for the given number as user input and uses while loop.
import java.util.Scanner; public class Reverse { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Reverse Number"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int remainder, temp, reverse=0; temp = number; while (temp!=0){ remainder = temp % 10; reverse = (reverse*10)+remainder; temp = temp / 10; } System.out.println("Reverse of Number '"+number+"'': "+reverse);; } }Output:
D:\Java_Programs>javac Reverse.java D:\Java_Programs>java Reverse Reverse Number ----------------------- Enter Number: 478 Reverse of Number '478'': 874
This java program is used to compute the reverse number for the given number as user input and uses do while loop.
import java.util.Scanner; public class Reverse { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Reverse Number"); System.out.println("-----------------------"); System.out.print("Enter Number: "); int number = in.nextInt(); int remainder, temp, reverse=0; temp = number; do{ remainder = temp % 10; reverse = (reverse*10)+remainder; temp = temp / 10; }while (temp!=0); System.out.println("Reverse of Number '"+number+"'': "+reverse);; } }Output:
D:\Java_Programs>javac Reverse.java D:\Java_Programs>java Reverse Reverse Number ----------------------- Enter Number: 153 Reverse of Number '153'': 351
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page