Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

cyter

What algorithm calculates the sum of digits in a number. Say you want to sum the digits in 4^10, what algorithm could you use?

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. kunwar_tej
    Best Response
    You've already chosen the best response.
    Medals 4

    You could use something like this : Lets say you have a number in num and want to add its digits and store in sum . for .e.g. num=12345,sum=0; 1. while num is greater than 0 do following else goto step 5 2. calculate sum as : sum=sum+num mod 10; 3. modify num num=num div 10; 4. goto step 1 5. print sum as result

    • 2 years ago
  2. rivermaker
    Best Response
    You've already chosen the best response.
    Medals 0

    I think the asker was interested in finding the answer when there is only an expression for the original number like 4^10 rather than 23897

    • 2 years ago
  3. farmdawgnation
    Best Response
    You've already chosen the best response.
    Medals 0

    rivermaker, perhaps - however it would be easy enough to write some code to take in the string "4^10" and then turn that into a real number, wouldn't it? Then the above algorithm would apply, yes? Also worth noting that the above algorithm will only work for whole numbers. If you have decimal points involved, I believe things get hairy.

    • 2 years ago
  4. kunwar_tej
    Best Response
    You've already chosen the best response.
    Medals 4

    i agree with farmdawgnation . i thought asker meant to find sum of digits of any number in general. sorry for misunderstanding that . but offcourse one anconvert that into a real number. And secondly, yes this algo will not work correctly for floating numbers.

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.