Friday, 6 December 2024

Day 21 : Python Program to Find Sum of Digit of a Number Without Recursion

 


def number_sum(number):

    total = 0
    number = abs(number)  
    while number > 0:
        total += number % 10 
        number //= 10         
    return total
number = int(input("Enter a number: "))
result = number_sum(number)
print(f"The sum of the digits of {number} is {result}.")

Code Explanation:

1. Function Definition

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

number_sum: The name of the function, which computes the sum of the digits of a number.
number: Input parameter representing the number whose digits will be summed.

2. Initializing the Total

    total = 0
total: A variable to store the cumulative sum of the digits. It is initialized to 0.

3. Handling Negative Input

    number = abs(number)
abs(number): Converts the number to its absolute value, removing any negative sign.
This ensures the program works for negative numbers by treating them as positive.

4. Iterative Summation
    while number > 0:
while number > 0:: Starts a loop that runs as long as number is greater than 0.
This ensures all digits are processed.

4.1 Extracting the Last Digit
        total += number % 10
number % 10: Extracts the last digit of number.

4.2 Removing the Last Digit

        number //= 10
number //= 10: Removes the last digit of number using integer division.

5. Returning the Result

    return total
return total: Returns the sum of the digits after the loop completes.

6. Taking User Input

number = int(input("Enter a number: "))
input("Enter a number: "): Prompts the user to enter a number.
int(): Converts the input (string) into an integer.
number: Stores the user-provided number.

7. Calling the Function

result = number_sum(number)
number_sum(number): Calls the number_sum function with the user-provided number.
result: Stores the returned sum of the digits.

8. Displaying the Result

print(f"The sum of the digits of {number} is {result}.")
print(f"..."): Displays the sum of the digits in a user-friendly format using an f-string.
The original number and its sum (result) are dynamically inserted into the 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