Computer Science, asked by gungunbajpai061105, 8 months ago

write a program to input a no. and display the reverse ( java using void main) format above...​

Attachments:

Answers

Answered by shilpanarzary04934
2

Answer:

There are three ways to reverse a number in Java.

1) Using while loop

2) Using for loop

3) Using recursion

4) Reverse the number without user interaction

Program 1: Reverse a number using while Loop

The program will prompt user to input the number and then it will reverse the same number using while loop.

import java.util.Scanner;

class ReverseNumberWhile

{

public static void main(String args[])

{

int num=0;

int reversenum =0;

System.out.println("Input your number and press enter: ");

//This statement will capture the user input

Scanner in = new Scanner(System.in);

//Captured input would be stored in number num

num = in.nextInt();

//While Loop: Logic to find out the reverse number

while( num != 0 )

{

reversenum = reversenum * 10;

reversenum = reversenum + num%10;

num = num/10;

}

System.out.println("Reverse of input number is: "+reversenum);

}

}

Output:

Input your number and press enter:

145689

Reverse of input number is: 986541

Program 2: Reverse a number using for Loop

import java.util.Scanner;

class ForLoopReverseDemo

{

public static void main(String args[])

{

int num=0;

int reversenum =0;

System.out.println("Input your number and press enter: ");

//This statement will capture the user input

Scanner in = new Scanner(System.in);

//Captured input would be stored in number num

num = in.nextInt();

/* for loop: No initialization part as num is already

* initialized and no increment/decrement part as logic

* num = num/10 already decrements the value of num

*/

for( ;num != 0; )

{

reversenum = reversenum * 10;

reversenum = reversenum + num%10;

num = num/10;

}

System.out.println("Reverse of specified number is: "+reversenum);

}

}

Output:

Input your number and press enter:

56789111

Reverse of specified number is: 11198765

Explanation:

hope it works.......

Answered by srimukhi123
0
\huge\blue{\mid{\fbox{\tt{HELLO}}\mid}}
&lt;body <br />bgcolor=red&gt;&lt;maruee&gt;&lt;font color=<br />Green&gt;
Hello
Similar questions