This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Finding sum of digits of a number until sum becomes single digit  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to solve the above mentioned problem Like if given number is 123, number of times required to convert it into a single digit number is 1 (1+2+3=6).



can you tell where i am going wrong ?
 
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a return statement, but no function signature. Please repost the code such that it shows the complete function.
 
priyanshi bhardwaj
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote: Please repost the code such that it shows the complete function.



 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My advice would be to step away from the code for a minute and write, in English, the list of steps to take to accomplish this. Then, and only then, write code to implement it.
 
priyanshi bhardwaj
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:My advice would be to step away from the code for a minute and write, in English, the list of steps to take to accomplish this. Then, and only then, write code to implement it.


I am facing problem in how to  calculate the no of times.Like sum can be calculated but how to calculate no of times the number will get replaced.I have referred geeksforgeeks for this problem GEEKSFORGEEKS I am not getting how to implement that explanation in javascript
 
Bear Bibeault
Marshal
Posts: 67256
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote: I am not getting how to implement that explanation in javascript


Do you understand the explanation? Are the steps to achieve the algorithm clear?

Again, write out, in your own words, how to solve the problem. Until you have that, any attempt to write code to implement an algorithm that is not understood will be futile.
 
priyanshi bhardwaj
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

priyanshi bhardwaj wrote: I am not getting how to implement that explanation in javascript


Do you understand the explanation? Are the steps to achieve the algorithm clear?

Again, write out, in your own words, how to solve the problem. Until you have that, any attempt to write code to implement an algorithm that is not understood will be futile.


I have tried this
 
Sheriff
Posts: 24369
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't much look like "your own words". That looks like some kind of programming.

But let's review it. Start by lines 2, 3, and 4. What is the explanation in words of those three lines?
 
lowercase baba
Posts: 12737
51
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are codinig, you should spend 90% of your time THINKING, and only 10% TYPING.  Never write a single line of code until you have a good idea in your head how you are going to solve the problem.

I generally start by writing out the steps I take in English, which is my native language.  Then I refine them, breaking them down into smaller and smaller pieces, making my description more and more detailed.  for this problem, it'd be something like this:

1)  get a number
2)  if the number is not a single digit
3)  add up all the digits in the number to get a new number
4)  return to step 2
5)  if it is a single digit, we're done.

Ok, so i look at that.  line 1 looks like it should be a method all to itself.  Based on the code sample you provided, i'd assume that's already done.
Now, lined 2-4 look like they should be a loop of some kind. Line 3 all by itself looks like it should be a method.

now I can see writing a method that has a loop to see if I have a single digit, and return that if so.  If not, I need to call another method that sums the digits.

So now I can break line 3 into individual steps.  It's a method all to itself, so it doesn't care about how many times I've tried, how long the result is, or anything. All it has to do is sum the digits in whatever number it is given.  Now I can code the method with the loop, or code this method.  Whichever way I go, I can write some temporary code as a harness to test the piece I'm working on.  Once I'm sure that piece works, I can then use it as I build the other pieces.

The idea is to separate out responsibility of tasks, so each method does one simple thing, does it well,  and can be easily understood.
 
Get out of my mind! Look! A tiny ad!
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!