Write a Java program to Check whether given number is Strong Number or Not ? | StudyEcart | CODE008

Understanding Strong Numbers:

Before we jump into the coding part, let's establish a clear definition of what strong numbers are. A strong number, also known as a factorial sum number, is a number that possesses a special property related to its digits and their factorials. When the sum of the factorials of its individual digits is equal to the number itself, it's deemed a strong number.


For instance, consider the number 145. Breaking it down:

1! + 4! + 5! = 1 + 24 + 120 = 145


Since the sum of the factorials of its digits equals the number itself, 145 is a strong number.


Java Program:

Now, let's take a hands-on approach and write a Java program that checks whether a given number is a strong number or not. Here's a step-by-step breakdown of the code:

 
  
  import java.util.Scanner;

public class StrongNumberChecker {
    // Function to calculate the factorial of a number
    static int factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    // Function to check if a number is a strong number
    static boolean isStrongNumber(int num) {
        int originalNum = num;
        int sumOfFactorials = 0;

        while (num > 0) {
            int digit = num % 10;
            sumOfFactorials += factorial(digit);
            num /= 10;
        }

        return sumOfFactorials == originalNum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        if (isStrongNumber(num)) {
            System.out.println(num + " is a Strong Number.");
        } else {
            System.out.println(num + " is not a Strong Number.");
        }
    }
}

  
(code-box)

Conclusion:

By now, you've not only gained an understanding of what strong numbers are but also learned how to write a Java program to identify them. Strong numbers offer a captivating intersection of mathematics and programming, making them a perfect challenge for coding enthusiasts.


Feel free to experiment with the code, test it with different numbers, and expand your programming skills. Happy coding!


🚀 Elevate Your Coding Journey with Studyecart! 📚

🔥 Unlock job opportunities with instant notifications and master programming languages like Java, Python, HTML, CSS, JavaScript, React, Node.js, and Spring Boot effortlessly.

📱 Download the app now: [Studyecart on Play Store](https://play.google.com/store/apps/details?id=in.studyecart.app)

🌟 Why Studyecart?

🎯 Stay Updated: Get the latest job notifications directly to your device.

🚀 Learn with Ease: Master complex coding languages through easy-to-follow courses.

🌐 Your Pace, Your Progress: Whether a beginner or pro, learn at your own pace and track your achievements.

Join Studyecart today and shape your coding success!

Post a Comment

0 Comments