Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Monday, 2 March 2015

Reverse of a given number using Java

 Earthcare Foundation NGO     08:14     Java     No comments   

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:

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.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates