Saturday, 7 December 2024

Day 19 : Python Program to Find the Smallest Divisor of an Integer

 


def smallest_divisor(number):
    number = abs(number)  
    if number <= 1:
        return "Input must be greater than 1."
    for i in range(2, number + 1):
        if number % i == 0:  
            return i  

number = int(input("Enter an integer: "))
result = smallest_divisor(number)
print(f"The smallest divisor of {number} is {result}.")


Code Explanation:

1. Function Definition

def smallest_divisor(number):
def: Declares a new function.

smallest_divisor: The name of the function, which finds the smallest divisor of a given number.
number: Input parameter representing the number for which the smallest divisor is to be found.
2. Taking Absolute Value

    number = abs(number)
abs(number): Returns the absolute value of the number (i.e., removes the negative sign if the number is negative).
Example: If number = -10, abs(-10) becomes 10.
This ensures the function works correctly for negative inputs.
3. Handling Small Numbers

    if number <= 1:
        return "Input must be greater than 1."
if number <= 1:: Checks if the input is 1 or less.
The smallest divisor is only meaningful for numbers greater than 1.
return: Ends the function and returns the message "Input must be greater than 1.".
4. Iterating Through Possible Divisors

    for i in range(2, number + 1):
for i in range(2, number + 1):
Loops through potential divisors, starting from 2 (the smallest prime number) up to the number itself.
range(2, number + 1): Generates integers from 2 to number (inclusive).
5. Checking for Divisibility

        if number % i == 0:
if number % i == 0:: Checks if i is a divisor of number by testing if the remainder when dividing number by i is 0.

6. Returning the Smallest Divisor

            return i
return i: Returns the first i that divides the number evenly (i.e., the smallest divisor).
The loop stops as soon as the smallest divisor is found.
7. Taking User Input

number = int(input("Enter an integer: "))
input("Enter an integer: "): Prompts the user to enter a number.
int(): Converts the input from a string to an integer.
number: Stores the user-provided number.
8. Calling the Function and Displaying the Result
result = smallest_divisor(number)
print(f"The smallest divisor of {number} is {result}.")
smallest_divisor(number): Calls the function with the user-provided number and stores the result in result.
print(f"..."): Displays the smallest divisor or the error message in a user-friendly format 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