Python Program to Find Sum of First and Last Digit
Last Updated :
18 May, 2023
Given a positive integer N(at least contain two digits). The task is to write a Python program to add the first and last digits of the given number N.
Examples:
Input: N = 1247
Output: 8
Explanation: First digit is 1 and Last digit is 7.
So, addition of these two (1 + 7) is equal to 8.
Input: N = 73
Output: 10
Method 1: String implementation
- Take input in the form of String or typecast given input in String.
- Now pick the 0th index of the String and typecast it into Integer and store it in a variable.
- The same thing with the -1st index and also store in another variable.
- Now add these two variables and
- print them as an Output.
Note: We can access the first element of String using string[0] and the last element of String using string[-1].
String Representation
Python3
# We have a number
number = 1247
# We are type casting it in string
number = str(number)
# Storing first and last digit in a variable
# after type casting into Integer.
first_digit = int(number[0])
last_digit = int(number[-1])
# Adding these two variables
addition = first_digit + last_digit
# Display our output
print('Addition of first and last digit of the number is',
addition)
OutputAddition of first and last digit of the number is 8
Output:
Addition of first and last digit of the number is 8
Time complexity: O(1) since performing constant operations
Auxiliary Space: O(1) since using constant space for variables
Method 2: Solve it using an integer
- We have given a positive Integer.
- After dividing by 10, store the remainder in a result variable.
- Continue the loop until the number becomes less than 9.
- Each time in the loop, divide the number by 10(integer division).
- After the end of the loop.
- Add the number in the result variable.
- Display the result variable as the output.
Note: Whenever we divide any number with 10, we get the last digit as the remainder. If we divide any number with 100, we get the last two-digit as the remainder.
Python3
# We have a number.
number = 1247
# Assigning last digit of the number in res
# variable.
res = number % 10
# Now, continue a loop until
# the number becomes less than 9.
while number > 9:
# integer division of the number and reassigning
# it.
number = number // 10
# Here, our number only contain one digit.
# So, add this number in res variable.
res += number
# Now, display our output
print('Addition of first and last digit of number is', res)
OutputAddition of first and last digit of number is 8
Output:
Addition of first and last digit of the number is 8
Time Complexity: O(n), where n is how many digits are there in the given number.
Auxiliary Space: O(1) since using constant space for variables
String Conversion and Digit Extraction Method
Steps:
- Take the input number as input from the user.
- Convert the input number to a string using the str() function.
- Extract the first digit by taking the first character of the string using the indexing operator.
- Extract the last digit by taking the last character of the string using the indexing operator.
- Convert the first and last digit from string to integer using the int() function.
- Add the first and last digit.
- Return the sum.
Python3
def sum_of_first_and_last_digit(n):
# convert the input number to a string
str_n = str(n)
# extract the first and last digit
first_digit = int(str_n[0])
last_digit = int(str_n[-1])
# add the first and last digit
sum = first_digit + last_digit
# return the sum
return sum
#Example
n = 1247
result = sum_of_first_and_last_digit(n)
print(result)
Time Complexity: O(1)
Auxiliary Space: O(1)
Method: Using Mathematical Formula
Steps:
- Take the input integer as N.
- Find the number of digits in N by taking the logarithm of N to the base 10 and adding 1 to it.
- Find the first digit of N by dividing N by 10 raised to (number of digits - 1).
- Find the last digit of N by taking the modulus of N with 10.
- Add the first and last digits to get the sum.
- Print the output.
Python3
import math
def sum_of_first_and_last_digit(N):
# find the number of digits in N
num_digits = int(math.log10(N)) + 1
# find the first digit of N
first_digit = N // (10**(num_digits-1))
# find the last digit of N
last_digit = N % 10
# add the first and last digits
# to get the sum
sum = first_digit + last_digit
# return the sum
return sum
# Driver Code
# Sample Input 1
N = 1247
# Calling function and printing output
print(sum_of_first_and_last_digit(N))
# Sample Input 2
N = 73
# Calling function and printing output
print(sum_of_first_and_last_digit(N))
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#5:using lambda
In this code, we take the input number from the user, convert it to an integer, and then use an anonymous lambda function to find the sum of the first and last digits of the number. The lambda function calculates the last digit using the modulus operator and the first digit using string manipulation.
Algorithm
- Take the input number from the user using the input() function.
- Convert the input string to an integer using the int() function.
- Define an anonymous lambda function that takes an integer argument x and calculates the sum of the first and last digits of x using the modulus operator and string manipulation.
- Pass the input number to the lambda function using the function call syntax (sum(n)).
- Print the sum of the first and last digits of the number using the print() function.
Python3
n = 1247
def sum(x): return x % 10 + int(str(x)[0])
print("Sum of first and last digit:", sum(n))
OutputSum of first and last digit: 8
Time complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Python program to find sum of elements in list Finding the sum of elements in a list means adding all the values together to get a single total. For example, given a list like [10, 20, 30, 40, 50], you might want to calculate the total sum, which is 150. Let's explore these different methods to do this efficiently.Using sum()sum() function is th
3 min read
Python program to find Cumulative sum of a list Calculating the cumulative sum of a list means finding the running total of the elements as we move through the list. In this article, we will explore How to find the cumulative sum of a list. Using itertools.accumulate()This is the most efficient method for calculating cumulative sums. itertools mo
3 min read
Python program to find the sum of all even and odd digits of an integer list The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893]
5 min read
Python Program to Get Sum of N Armstrong Number Given a number N, determine the sum of the first N Armstrong numbers using Python. Example: Input : 11Output : 568First 11 Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, lies to, 370Their summation is 578Method 1: Using Iterative methodsCreate a while loop that breaks when the desired number of Ar
3 min read
Find Sum and Average of List in Python Our goal is to find sum and average of List in Python. The simplest way to do is by using a built-in method sum() and len(). For example, list of numbers is, [10, 20, 30, 40, 50] the sum is the total of all these numbers and the average is the sum divided by the number of elements in the list.Using
2 min read