W.A.P to input a number. Display all numbers in reverse form if it is an even number in JAVA.
Answers
Answer:
import java.util.Scanner;
class ReverseNumberEven
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
Scanner in = new Scanner(System.in);
num = in.nextInt();
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
if (reversenum%2==0)
{
System.out.println("Reverse of input number is: "+reversenum);
else
System.out.println("Enter even number");
}
}
}
Question:-
- W.A.P to input a number. Display all numbers in reverse form if it is an even number in JAVA.
Code:-
package Coder;
import java.util.*;
public class EveReverse
{
public static void main (String ar [])
{
System.out.println("Enter a number");
int n=sc.nextInt();
int r=0;
if(n%2==0)
{
while (n!=0)
{
r=r*10+(n%10); n/=10;
}
System.out.println("Number is even, Reverse if the number is:"+r);
}
else
System.out.println("Number not Even");
}
} //ShortestCodePossible
Variable Description:-
- n:- to accept the number as input
- r:- to reverse the number if it's even
Code Explanation:-
- The program accepts the user to input 1 number
- Then it checks if the number is even or not.
- if even then it reverses the number and store it in variable 'r'.