Forums Register Login

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

+Pie Number of slices to send: Send
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 :
+Pie Number of slices to send: Send
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.
    +Pie Number of slices to send: Send
    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
    +Pie Number of slices to send: Send
    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
    +Pie Number of slices to send: Send
     

    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..
    Your mother is a hamster and your father smells of tiny ads!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 8365 times.
    Similar Threads
    Enter a number and print if it's an Armstrong number or not : help me remove the error.
    problem with while loop
    need a little help
    fun with integers!
    Can't figure out how to approach this program
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 02:53:48.