Results 1 to 8 of 8
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

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



    Thank you in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,414
    Blog Entries
    7
    Rep Power
    25

    Default

    All you have to do is change this line:

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

    Java Code:
    String dsum = "";
    ... and change your loop somewhat if n == 0 to start with ...

    kind regards,

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

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    Uhm, you posted the wrong code, that sums up the digits, but does not reverse anything.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    7

    Default

    Could this work?

    Break String to Char Array, then print reverse?
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  5. #5
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    can anybody solve it?

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    Jos has solved it 12 days ago.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  7. #7
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

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

  8. #8
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Yes Josah and phhein it works.thank you guys much appreciated.

Similar Threads

  1. Trying to get JAVA to work with SQLite
    By mark8569 in forum JDBC
    Replies: 3
    Last Post: 04-25-2009, 01:42 AM
  2. Just how do I get Java to actually work?
    By MickY G in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 04:50 AM
  3. What are applet and how its work in java?
    By pawankumarom in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 05:26 PM
  4. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM
  5. Ho to work with enumerations in java
    By zizou147 in forum Advanced Java
    Replies: 0
    Last Post: 03-22-2008, 01:53 PM

Posting Permissions

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