Saturday, 7 December 2024

Day 16 : Python program to check whether a number is strong number

 


import math

def is_strong_number(number):

    sum_of_factorials = sum([math.factorial(int(digit)) for digit in str(number)])

    return sum_of_factorials == number

# Input from user

num = int(input("Enter a number: "))

if is_strong_number(num):

    print(f"{num} is a strong number.")

else:

    print(f"{num} is not a strong number.")


Code Explanation: 

Importing the Math Library

import math

import math: Brings the math library into the program, giving access to mathematical functions like math.factorial().

2. Defining the Function

def is_strong_number(number):

def: Declares a new function.

is_strong_number: The name of the function, which determines if a given number is a strong number.

number: The input parameter for the function, representing the number to check.

3. Calculating the Sum of Factorials of Digits

    sum_of_factorials = sum([math.factorial(int(digit)) for digit in str(number)])

str(number): Converts the number into a string so that we can loop through its digits.

int(digit): Converts each digit (originally a string) back to an integer.

math.factorial(int(digit)): Calculates the factorial of the digit.

sum([...]): Calculates the sum of the list.

sum_of_factorials: Stores the result of this calculation.

4. Checking if the Number is a Strong Number

   return sum_of_factorials == number

sum_of_factorials == number: Compares the sum of the factorials of the digits to the original number.

If they are equal, the number is a strong number.

Returns True if the number is strong; otherwise, returns False.

5. Taking Input from the User

num = int(input("Enter a number: "))

input("Enter a number: "): Displays a prompt and takes input from the user as a string.

int(): Converts the string input to an integer.

num: Stores the user-provided number.

6. Checking and Printing the Result

if is_strong_number(num):

    print(f"{num} is a strong number.")

else:

    print(f"{num} is not a strong number.")

if is_strong_number(num):: Calls the is_strong_number function with the user’s input (num).

If the function returns True, it means num is a strong number.

If it returns False, num is not a strong number.

print(f"..."): Displays the result to the user using an f-string.


   #source code --> clcoding.com 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (28) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (4) PHP (20) Projects (29) Python (923) Python Coding Challenge (318) Python Quiz (4) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses