Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Logical

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logical

    How to find sum of digits without using any "loops","recursion","goto"...
    thanks


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Logical

    Can you give me an idea of what you have already and what code you have tried please.

  3. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Logical

    add them together?

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Logical

    Think logically how you would do this. On paper you would write down the numbers and then add each one to the next till you have a total.
    In a computer you have to first "write" down the numbers by letting them equal something first so that the computer knows what you are doing.
    Do you have to add randoms numbers that the user inputs or do you have to add numbers that you already have?
    This makes a massive difference.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logical

    thanq...

    the input should be from the user


    Actually want i am asking is


    Ex:

    input-----------123456
    output---------1+2+3+4+5+6=21

    --- Update ---

    i have tried and i can't get any ideas about that...
    Thankq

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Logical

    Try looking at breaking apart the massive thing first, from there on in it becomes easy.
    1. break up into the smaller numbers
    2. add the smaller numbers
    3. save this total
    4. display this total

    read up a bit about chars. I think that will help you. I am a beginner with coding in general but i Think that should help.

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Logical

    Not sure if you can do this (I'm fairly new) and I can't try this at work, but here's what I would try:
    1. Read the input in as a string rather than an int
    2. Use the charAt Method to read each character individually
    3. After you read the individual characters, use the Integer wrapper class to convert the character back to an integer
    4. Add up the integers

    Advice from one newbie to the next... it makes sense in my head, at least...

  8. #8
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Logical

    there is another thread here going with the same question asked and has some other info thats not in here that you should look at

    Consecutive number addition

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logical

    @kissyfurs
    Thanku for the idea... but i've alredy tried that... and we should use loops to know character at a particular position.....
    my intention is not to use loops and recursion and goto

    @derekxec
    Thanq...but its not same as mine..

  10. #10
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Logical

    why couldn't you do that without the loop? You'd have to write the code out 6 different times and manually change the charAt each time, but it would work. Why are you trying to avoid loops? Is there another method that you're supposed to use?

  11. #11
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logical

    @kissyfurs

    thanq so much for the help.

    Actually it is an interview question. i'm just trying to answer that...

Similar Threads

  1. logical error in output
    By siyanor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 28th, 2013, 04:22 AM
  2. Logical Problem
    By dougie1809 in forum AWT / Java Swing
    Replies: 18
    Last Post: February 21st, 2013, 01:17 PM
  3. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  4. I have a logical error
    By n00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 02:52 PM
  5. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 05:26 PM