Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

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

Greenhorn
Posts: 10
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 :

Marshal
Posts: 57464
175
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
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

Bartender
Posts: 585
9
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: 57464
175

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..

 It is sorta covered in the JavaRanch Style Guide.