Jump to content

Help needed for Java Programming

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
Hye, im pwincy here...i need help on how to write a program to display the reverse of five digit number entered by users as input.

for example: if number entered is 12345, the result should be 54321.

using: -modulus(%) operator
-loop to divide number each time by 10.

Finally, i should get the remainder each time and store it in a variable.

Can any please help me on this.

Thanks.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
i'll give you 2 tips:
  • doing %10 on a number returns the last digit.
    12345%10 == 5
  • dividing an integer by 10 removes the last number from the integer
    12345/10 == 1234

What do you mean with

Quote

i should get the remainder each time


#3
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
it means that i have to get the remainder each time i divide the number with 10.

#4
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
import java.util.Scanner; // program uses class Scanner

public class ReverseDigits
{
static Scanner console = new Scanner(System.in);

public static void main( String args[])
{


int number;

System.out.println("Enter an integer");
number = console.nextInt();

reverse (number); // print the method reverse


} // end method main

public static void reverse ( int num )

{

int lastDigit; // the last digit returned when reversed
int reverse = 0;

do
{
lastDigit = num % 10;
reverse = (reverse * 10) + lastDigit;
num = num / 10;
}
while (num > 0);

System.out.println("The integer with its digits reversed " + reverse);

}
}


This is my source code that i have done. Is there any mistake? Can you please help tell me if there's any mistake on the code above.

Thanks.

Edited by ZekeDragon, 31 October 2010 - 09:26 AM.
Please use [code] tags when posting code.


#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
It works perfectly i would only structure the class differently by putting the scanner in the main, and letting reverse return the integer. But that's just my taste ^^

Something else i would change is the do-while into a while-loop. In this case there's no reason for the loop to execute at least once.

You could also let the condition be (num != 0) Then negative numbers would also get reversed.
-12345 --> -54321


public class ReverseDigits

{


public static void main( String args[])

{

  Scanner console = new Scanner(System.in);

  int number;


  System.out.println("Enter an integer");

  number = console.nextInt();


  System.out.println(reverse (number)); // print the method reverse



} // end method main


public static int reverse ( int num )

{


  int lastDigit; // the last digit returned when reversed

  int reverse = 0;


  while(num>0)

  {

  lastDigit = num % 10;

  reverse = (reverse * 10) + lastDigit;

  num = num / 10;

  }  


  System.out.println("The integer with its digits reversed " + reverse);

}


}




#6
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
okay...thanks alot =)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users