• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on a program trying to compute all the factors  RSS feed

 
Sri Harsha Bolisetti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I am trying to write a program for finding all the factors of a number but I am stuck here. There is some thing wrong with the logic I am not able to identify. When I submit this solution in mycodeschool I am getting error as output is wrong.

Input

36
48
15
19

Expected Output

1 2 3 4 6 9 12 18 36
1 2 3 4 6 8 12 16 24 48
1 3 5 15
1 19

Your Output

1 2 3 4 6 9 12 18 36
1 2 3 4 6 12 16 24 48
1 3 15
1 19



For 15 as input my program was not able to detect 5 as a factor and for 48 it was not able to detect 8.
I know there's some silly mistake but wasn't able to find it.

Here's my code



Thanks in advance.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For 15, (int) Math.sqrt(15) = 3 so you will not find 5 with that condition.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) StopCoding (<---click that)

2) turn off your computer

3) Think through the problem. Run through some examples LITERALLY with paper and pencil.

4) Try explaining to a 10 yr old child the steps to take.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!