• Post Reply Bookmark Topic Watch Topic
  • New Topic

Input a number and print the sum of the factorials of it's digits and check if it is the num itself  RSS feed

 
Akshat Shree Ram Pandey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there dudes!
Hope ya'll are well, I attempted another Iteration question and though I think the program is correct, the result is always 0 which means the program results in
Number is not a Special Number
Okay, here's the question :
Write a Program in Java to accept a number using Scanner and calculate the sum of the factors of it's digits and print if the sum of the factors is equal to the entered number
For an example, let's take 145!
first digit : 1, Factorial = 1
second digit : 4 Factorial =24
third digit : 5 Factorial = 120
Sum of factorials =    1
                            +24
                           +120
                           -------
                         =  145 
                         = Entered number itself.
So here's my attempted code :
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you adding the factorials together? When you calculate the factorials, what value are you starting with?

There is all sorts of poor design there:
  • 1: You  have written 100% procedural code. Maybe you are being taught procedural code, and you can get away with it for an algorithm on its own, but Java┬« is supposed to be an object language.
  • 2: You ought to move as much code out of the main method as possible.
  • You also have style problems, with inconsistent indentation, not enough {}, and code without enough spaces between tokens, e.g. line 9.
     
    Akshat Shree Ram Pandey
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yeah I am compelled to use procedural code,
    The style problem is actually a style itself Herbert Schildt the Author of The complete Reference series uses it himself, so I consider it good and it saves lines too!
    Erm.. can you point out my mistake?
    I am starting with 0.
    Didn't you see my example of 145 above?
    Thanking you for the reply !
    - ARSP
     
    Fred Kleinschmidt
    Bartender
    Posts: 560
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since you are only interested in the factorials of the digits 0-9, you should consider calculating them once and placing the result in an array .Then for each digit in your input number you just look up the value in the array instead of re-computing the value.

    You could also do a "lazy computation", presetting the values to zero and just computing the value only if it has not already been set. For example
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Akshat Shree Ram Pandey wrote:Yeah I am compelled to use procedural code,
    The style problem is actually a style itself Herbert Schildt the Author of The complete Reference series uses it himself, so I consider it good and it saves lines too!
    Erm.. can you point out my mistake?
    . . .
    I shan&aps;t be able to check my Schildt until Tuesday.

    Your first post says your program prints 0 regardless. Go through how you are calculating ldf and for each stage work out what its value is. You will have up to 10 different values to deal with. If you have corrected your code, please post the corrected version..
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!