In this post i will explain how to calculate reverse of the given number.Suppose given number is 234.Then what will be the reverse of this given number?
Answer is 432.
It is easy to find out the reverse of any given number using pen and paper, but if you want to calculate the reverse using a program then how will you write the program.
Lets think about it, if we want to reverse any number then what is our first task. Our first task is to get the last digit and put it at first, So for this what can we do?
If number is 234 then how will we get the last digit using expression.The answer is we can applu the % operator so that we will get the last digit of a number.
if number=234
int remainder=number%10
The above expression will give us last digit of a number that is 4
After this since we have extracted the last digit now we don't need it.So we have to find out the way to remove this last digit from number.How this can be done?
The answer is, if we will divide any number by 10 then we can remove last digit from number
so number=number/10
then number=23
So we have get the last digit in result and removed last digit from number.
Now we have a problem, when we find the reverse of a number then we do the following:
234= 400*30*2 = 432
But how will we add number of 0s in our program.I can suggest you a simple way, we can use a String variable in which only we have to concatenate the digits we are getting in remainder.
String res=res+remainder;
So after first step res=4 remainder=4 and number=23
now again performing those steps,
remainder=number%10 = 3
number=number/10=2
res=res+remainder=43
So in this way we can find out the remainder of any given number.Below is complete program:
Answer is 432.
It is easy to find out the reverse of any given number using pen and paper, but if you want to calculate the reverse using a program then how will you write the program.
Lets think about it, if we want to reverse any number then what is our first task. Our first task is to get the last digit and put it at first, So for this what can we do?
If number is 234 then how will we get the last digit using expression.The answer is we can applu the % operator so that we will get the last digit of a number.
if number=234
int remainder=number%10
The above expression will give us last digit of a number that is 4
After this since we have extracted the last digit now we don't need it.So we have to find out the way to remove this last digit from number.How this can be done?
The answer is, if we will divide any number by 10 then we can remove last digit from number
so number=number/10
then number=23
So we have get the last digit in result and removed last digit from number.
Now we have a problem, when we find the reverse of a number then we do the following:
234= 400*30*2 = 432
But how will we add number of 0s in our program.I can suggest you a simple way, we can use a String variable in which only we have to concatenate the digits we are getting in remainder.
String res=res+remainder;
So after first step res=4 remainder=4 and number=23
now again performing those steps,
remainder=number%10 = 3
number=number/10=2
res=res+remainder=43
So in this way we can find out the remainder of any given number.Below is complete program:
Program Code:
package examples;
import java.util.Scanner;
public class Reverse {
private static String calcReverse(int number) {
String res="";
while(number>0){
int remainder=number%10;
res=res+remainder;
number=number/10;
}
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number to reverse \n");
Scanner in=new Scanner(System.in);
int number=in.nextInt();
String result=calcReverse(number);
System.out.println("Reverse of given number is "+result);
}
}
Output:
Enter the number to reverse
123567
Reverse of given number is 765321
So above is all about finding reverse of any given number.package examples;
import java.util.Scanner;
public class Reverse {
private static String calcReverse(int number) {
String res="";
while(number>0){
int remainder=number%10;
res=res+remainder;
number=number/10;
}
return res;
}
public static void main(String[] args) {
System.out.println("Enter the number to reverse \n");
Scanner in=new Scanner(System.in);
int number=in.nextInt();
String result=calcReverse(number);
System.out.println("Reverse of given number is "+result);
}
}
Output:
Enter the number to reverse
123567
Reverse of given number is 765321
0 comments:
Post a Comment