Loading...

What is Strong Number in Java?

What is Strong Number in Java?

Strong Number, Armstrong Number, and Perfect Number what are those numbers how do they differ from each other, and how to implement them using Java, all these we are going to learn shortly.

Introduction

What is a Strong Number? Does that mean it has more strength or muscle power compared to others? No, if you think that, it doesn’t mean you are wrong, it just shows your sense of humor. well, all this stuff apart, let’s learn something useful.

Bookish Definition

Well, let’s see what books say about a strong number. Not only books every programmer you ask will reply to you with this one sentence, A strong number is a number whose sum of the factorial of individual digits is equal to the original number. I know, you are not here for the bookish definition. so, let’s understand its meaning with an example.

Illustration

If you take the number 145 the factorials of 1,4,5 are 1,24,120 which when added gives 145.

Now that we have learned what it means, let us see its implementation in java.

Implementation in Java

Why java? Can’t we do it in another language? yes, we can.

Let us see the algorithm.

Algorithm

  • Take a number as input, store it in an integer variable, and name it num.
  • Extraction of digits
    • loops through the given statements while num is not equal to 0.
      1. Divide num by 10 and store the remainder, the last digit of the number.
      2. now rewrite the num with the quotient.
  • Finding Factorial
    • Initialize fact to 0.
    • loop through the given statements till I is less than the digit obtained from the above snippet.
      1. multiply the fact variable with I.
      2. increment I by 1 for every iteration.
  • Add all the factorials obtained from the above snippet and store them in a variable named sum.
  • if Sum is equal to the original input then print the Strong Number.
  • else print not a strong number.

Code

import java.util.Scanner; public class MyClass { static int factorl(int z) { if(z ==0) { return 1; } else { return z*factorl(z-1); } } public static void main(String args[]) { int mun,digie,tempr; Scanner in = new Scanner(System.in); mun = in.nextInt(); tempr=mun; int Aggregate=0; while(mun!=0) { digie = mun%10; Aggregate+=factorl(digie); mun/=10; } if(Aggregate == tempr) { System.out.println("Given Number is a Strong Number"); } else { System.out.println("Given Number is not a Strong Number"); } } }
Code language: Java (java)

Input and Output#1

145 Given number is a Strong Number.
Code language: Java (java)

Input and Output#2

123 Given number is not a Strong Number.
Code language: Java (java)

Analysis Through Example

#1: Let us consider the example of 145. It is taken as input and stored in variable mun. we next copied mun into tempr. In every loop, the digit is extracted and sent to the factorl function which returns the factorial value of digits. finally, the factorial of all digits is stored in variable aggregate. now, this aggregate equals tempr which is the original number, So it is a strong number.

tempr = 145 Aggregate = 1!+4!+5! Aggregate = 1+24+120 Aggregate = 145 Aggregate == temp Hence, it is a Strong Number.
Code language: Java (java)

#2: Let us consider another example of 123. It is taken as input and stored in variable mun. we next copied mun into tempr. In every loop, the digit is extracted and sent to the factorl function which returns the factorial value of digits. finally, the factorial of all digits is stored in variable aggregate. now, this aggregate equals tempr which is the original number, So it is a strong number.

tempr = 123 Aggregate = 1!+2!+3! Aggregate = 1+2+6 Aggregate = 9 Aggregate != temp Hence, it is not a Strong Number.
Code language: Java (java)

Conclusion

Now that we have understood what a strong number is, we have also learned how to extract digits from a number and find their factorials which can be used in various coding problems. And one more thing, practice writing code on your own such that you can easily spot the difference between Strong, Armstrong, and Perfect numbers.

That’s it for now, Happy Learning.

Sharing is caring

Did you like what Krishna Bethina wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far