Sum of First and Last Digit of a Number in Python
Here, I will show you sum of first and last digit in python. step by step explain sum of first and last digit python. This example will help you python sum of first and last digit of number. I explained simply step by step python program to find sum of first and last digit of a number.
There are several ways to sum first and last digit of number in python. Here's an example code in Python to find the sum of the first and last digits of a given integer:
number1 = 43567 # Get First Digit from Number lastDigit = str(number1) lastDigit = int(lastDigit) # Get Last Digit from Number firstDigit = str(number1)[-1] firstDigit = int(firstDigit) sum = firstDigit + lastDigit print(sum)
num = int(input("Enter an integer: ")) # get input integer # convert integer to string to access individual digits num_str = str(num) # get the first and last digits first_digit = int(num_str) last_digit = int(num_str[-1]) # calculate the sum of the first and last digits sum = first_digit + last_digit print("The sum of the first and last digits of", num, "is", sum)
In this code, we first prompt the user to input an integer. We then convert the integer to a string so that we can access the individual digits. We get the first digit using num_str and the last digit using num_str[-1]. Finally, we add the first and last digits and store the result in the variable sum, which we then print out.
I hope it can help you...
We are Recommending you
- How to Get the Last Digit of a Number in Python?
- How to Get the First Digit of a Number in Python?
- Python Remove All Decimals from Number Example
- Python Convert String to Float with 2 Decimal Places Example
- Python Format Number to 2 Decimal Places Example
- How to Sort List Descending in Python?
- How to Reverse a List in Python?
- Python Write CSV File from List Example
- How to Check If a List is Empty or Not in Python?
- Python Convert List into String with Commas Example
- How to Remove Last Element from List in Python?
- How to Remove First Element from List in Python?
- Python Delete Directory if Exists Example
- How to Check if Today is Wednesday or not in Python?