+ 1

logic to display reverse no in java

by using loop we use the logic but how exactly we display..❓

28th Jun 2017, 3:32 AM
Akshata Biradar
3 Answers
+ 1
while (n!=0) { x=(x*10)+n%10; n=n/10; } it extracts the last number one by one from the number and adding it to a new variable while the number is shortened everytime by removing the last digit
28th Jun 2017, 3:49 AM
Nirmal Kumar Bhakat
Nirmal Kumar Bhakat - avatar
+ 1
you can also accept the number as string and simply print it reversely
28th Jun 2017, 4:08 AM
Vaibhav Tandon
Vaibhav Tandon - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); for(int i=arr.length-1;i>=0;i--) { System.out.println(arr[i]); }// Just we start printing from back side } }
14th Jun 2021, 4:23 PM
Dharmi Sri
Dharmi Sri - avatar