Tutorialspoint
Problem
Solution
Submissions

Strong Number Checking

Certification: Basic Level Accuracy: 75% Submissions: 4 Points: 10

Write a C++ program to check if a given number is a strong number. A strong number is a number where the sum of the factorial of its digits is equal to the number itself.

Example 1
  • Input: number = 145
  • Output: true
  • Explanation:
    • Step 1: Calculate the factorial of each digit in 145.
    • Step 2: 1! = 1, 4! = 24, 5! = 120.
    • Step 3: Sum the factorials: 1 + 24 + 120 = 145.
    • Step 4: Since the sum 145 equals the original number 145, return true.
Example 2
  • Input: number = 123
  • Output: false
  • Explanation:
    • Step 1: Calculate the factorial of each digit in 123.
    • Step 2: 1! = 1, 2! = 2, 3! = 6.
    • Step 3: Sum the factorials: 1 + 2 + 6 = 9.
    • Step 4: Since the sum 9 does not equal the original number 123, return false.
Constraints
  • 1 ≤ number ≤ 10^6
  • Time Complexity: O(n), where n is the number of digits in the number
  • Space Complexity: O(1)
NumberControl StructuresCapgeminiGoldman Sachs
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Calculate the factorial of each digit of the number.
  • Sum the factorials and compare the result with the original number.
  • Handle edge cases where the number is 0 or 1 separately.

Steps to solve by this approach:

 Step 1: Define a factorial function that calculates n! using recursion.

 Step 2: Define a function is_strong_number that takes an integer.
 Step 3: Store the original number for comparison.
 Step 4: Initialize a sum variable to 0.
 Step 5: Extract each digit using modulo and division operations.
 Step 6: Calculate the factorial of each digit and add it to the sum.
 Step 7: Compare the sum with the original number and return true if they're equal.

Submitted Code :