# Thread: Java course work beginner's leve,Help

1. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0

## Java course work beginner's leve,Help

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);
}
}

2. All you have to do is change this line:

Java Code:
`int dsum = 0;`
... to this:

Java Code:
`String dsum = "";`

kind regards,

Jos
Last edited by JosAH; 05-11-2010 at 02:58 PM.

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

4. Could this work?

Break String to Char Array, then print reverse?

5. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
can anybody solve it?

6. Jos has solved it 12 days ago.

7. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Thank you for the replay phhein.Has it been really solved ? i need to try it yet.cheers.

8. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Yes Josah and phhein it works.thank you guys much appreciated.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•