how to reverse number
HOW TO REVERSE A NUMBER
take varible
- int number; //to store original or to get quotient
- int rev; //to decrease the number
- int reverse_number = 0; //to store reverse number
- First take input from user.(number=5364)
- then run one while loop condition while(number>0)
- under loop rev = number%10;
- //we get rev =536
- //take another variable reverse_number
- reverse_number = reverse_number*10+rev;
- //we get reverse_number= 4
- number=number/10;
- //now number= 536
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// reverse number program
int number,rev,reverse_number=0;
System.out.println("Enter a number");
Scanner sc= new Scanner(System.in);
number=sc.nextInt();
while (number>0) {
rev = number % 10;
reverse_number = reverse_number * 10 + rev;
number = number/ 10;
}
System.out.println(reverse_number);
}
}
Enter a number
569
965
Comments
Post a Comment