# Logical

• July 15th, 2013, 12:07 PM
snarayana.murthy86
Logical
How to find sum of digits without using any "loops","recursion","goto"...
thanks
• July 15th, 2013, 12:15 PM
Seraphim
Re: Logical
Can you give me an idea of what you have already and what code you have tried please.
• July 15th, 2013, 12:18 PM
derekxec
Re: Logical
• July 15th, 2013, 12:24 PM
Seraphim
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.
• July 15th, 2013, 12:32 PM
snarayana.murthy86
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
• July 15th, 2013, 12:36 PM
Seraphim
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
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.
• July 15th, 2013, 12:40 PM
kissyfurs
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

Advice from one newbie to the next... it makes sense in my head, at least...
• July 15th, 2013, 01:06 PM
derekxec
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

• July 15th, 2013, 01:34 PM
snarayana.murthy86
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..
• July 15th, 2013, 02:21 PM
kissyfurs
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?
• July 16th, 2013, 01:08 AM
snarayana.murthy86
Re: Logical
@kissyfurs

thanq so much for the help.

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