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.
5 replies to this topic
#1
Posted 31 October 2010 - 12:03 AM
|
|
|
#2
Posted 31 October 2010 - 01:41 AM
i'll give you 2 tips:
What do you mean with
- 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
Posted 31 October 2010 - 04:42 AM
it means that i have to get the remainder each time i divide the number with 10.
#4
Posted 31 October 2010 - 04:48 AM
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
Posted 31 October 2010 - 06:31 AM
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
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
Posted 01 November 2010 - 08:28 AM
okay...thanks alot =)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









