Hi Friends,
This is my first time posting in this form and first time posting with regards to Java.I am new learner of Java.And I need help please.

Write a JAVA program that asks for a number (for instance 154).The program
has to display the digits of the number in reverse order.

for instance: 451

I hope you got the question,another example is suppose I entered 234 as the input on the program.The program has to display 432.

The program below should give u clues.I tried many times but I was unsuccessful.
/*
* File: DigitSum.java
* -------------------
* This program sums the digits in a positive integer.
* The program depends on the fact that the last digit of
* a integer n is given by n % 10 and the number consisting
* of all but the last digit is given by the expression n / 10.
*/
import acm.program.*;
public class DigitSum extends ConsoleProgram {
public void run() {
println("This program sums the digits in an integer.");
int n = readInt("Enter a positive integer: ");
int dsum = 0;
while (n > 0) {
dsum += n % 10;
n /= 10;
}
println("The sum of the digits is " + dsum);
}
}

Thank you in advance.

05-11-2010, 03:52 PM

JosAH

All you have to do is change this line:

Code:

int dsum = 0;

... to this:

Code:

String dsum = "";

... and change your loop somewhat if n == 0 to start with ...

kind regards,

Jos

05-11-2010, 03:52 PM

PhHein

Uhm, you posted the wrong code, that sums up the digits, but does not reverse anything.

05-11-2010, 09:18 PM

Sno

Could this work?

Break String to Char Array, then print reverse?

05-17-2010, 05:31 PM

ccie007

can anybody solve it?

05-17-2010, 05:52 PM

PhHein

Jos has solved it 12 days ago.

05-17-2010, 05:59 PM

ccie007

Thank you for the replay phhein.Has it been really solved ? i need to try it yet.cheers.