How to find sum of digits without using any "loops","recursion","goto"...
thanks
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.
How to find sum of digits without using any "loops","recursion","goto"...
thanks
Can you give me an idea of what you have already and what code you have tried please.
add them together?
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.
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
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.
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...
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
http://www.javaprogrammingforums.com...-addition.html
@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..
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?
@kissyfurs
thanq so much for the help.
Actually it is an interview question. i'm just trying to answer that...