Problem Link - First and Last Digit Practice Problem in 500 difficulty rating
Problem Statement:
Given an integer N . Write a program to obtain the sum of the first and last digits of this number.
Approach:
- Extracting the Last Digit: The last digit of
N
can be found using the modulus operation:last_digit = N % 10
. - Extracting the First Digit: To find the first digit, divide
N
by10
repeatedly untilN
becomes less than10
. The remaining value is the first digit. - Calculating the Sum: Calculate the sum of the first and last digits and print the result for each test case.
Complexity:
- Time Complexity:
O(log N)
represents the number of divisions needed to extract the first digit. - Space Complexity:
O(1)
, as we are only using basic variables for calculations.