Given a number which is integer , the task is to create an integer with the number of digits at ten’s place and the least significant digit of the entered integer at one’s place.
Examples:
Example1:
Input:
given number = 37913749
Output:
The required number = 89
Example2:
Input:
given number =78329942
Output:
The required number = 82
Create an integer with the number of digits at ten’s place and the least significant digit of the entered integer at one’s place in Python
There are several ways to create an integer with the number of digits at ten’s place and the least significant digit of the entered integer at one’s place some of them are:
- Using while loop and String Conversion ( Static Input)
- Using while loop and String Conversion ( User Input)
- Using len() and indexing by converting number to string (Static Input)
Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.
Method #1:Using while loop and String Conversion ( Static Input)
Approach:
- Give a number as static input and store it in a variable.
- Make a duplicate of the integer.
- Take a variable which stores the count of digits and initialize it with 0
- Using a while loop, calculate the number of digits in the given integer.
- Convert the integer copy and the number of digits count to string.
- Concatenate the integer’s last digit with the count.
- The newly created integer should be printed.
- Exit of program.
Below is the implementation:
# given number numb numb = 37913749 # Taking a temporary variable which stores the given number tempNum = numb # Take a variable which stores the count of digits and initialize it with 0 countTotalDigits = 0 # looping till the given number is greater than 0 while(numb > 0): # increasing the count of digits by 1 countTotalDigits = countTotalDigits+1 # Dividing the number by 10 numb = numb//10 # converting the temporary variable to string strnum = str(tempNum) # Extracting least significant digit leastSigDigit = strnum[-1] # converting the count Total Digits to string countTotalDigits = str(countTotalDigits) # calculating required number resnum = countTotalDigits+leastSigDigit print("The required number =", resnum)
Output:
The required number = 89
Explanation:
- Give a number as static input and store it in a variable.
- Because the original value of the variable would be modified for counting the number of digits, a copy of the variable is created.
- The while loop is employed, and the modulus operator is utilized to get the last digit of the number.
- The count value is incremented each time a digit is obtained.
- The variable’s number of digits and the number are transformed to string for simplicity of concatenation.
- The string’s final digit is then retrieved and concatenated to the count variable.
- The newly generated number is then printed.
Method #2:Using while loop and String Conversion ( User Input)
Approach:
- Scan the given number using int(input()) and store it in a variable.
- Make a duplicate of the integer.
- Take a variable which stores the count of digits and initialize it with 0
- Using a while loop, calculate the number of digits in the given integer.
- Convert the integer copy and the number of digits count to string.
- Concatenate the integer’s last digit with the count .
- The newly created integer should be printed.
- Exit of program.
Below is the implementation:
# given number numb numb = int(input("enter any number = ")) # Taking a temporary variable which stores the given number tempNum = numb # Take a variable which stores the count of digits and initialize it with 0 countTotalDigits = 0 # looping till the given number is greater than 0 while(numb > 0): # increasing the count of digits by 1 countTotalDigits = countTotalDigits+1 # Dividing the number by 10 numb = numb//10 # converting the temporary variable to string strnum = str(tempNum) # Extracting least significant digit leastSigDigit = strnum[-1] # converting the count Total Digits to string countTotalDigits = str(countTotalDigits) # calculating required number resnum = countTotalDigits+leastSigDigit print("The required number =", resnum)
Output:
enter any number = 78329942 The required number = 82
Method #3: Using len() and indexing by converting number to string (Static Input)
Approach:
- Give a number as static input and store it in a variable.
- Using the str() method, convert the given number to a string.
- Determine the number of digits in the given number by computing the length of the string with the len() function.
- Concatenate the integer’s last digit with the count.
- The newly created integer should be printed.
- Exit of program.
Below is the implementation:
# given number numb numb = 82179 # Using the str() method, convert the given number to a string. strnum = str(numb) # Determine the number of digits in the given number by computing # the length of the string with the len() function. countTotalDigits = len(strnum) # Extracting least significant digit leastSigDigit = strnum[-1] # converting the count Total Digits to string countTotalDigits = str(countTotalDigits) # calculating required number resnum = countTotalDigits+leastSigDigit print("The required number =", resnum)
Output:
The required number = 59
Related Programs:
- Python Program to Count the Number of Digits Present In a Number
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Python Program to Form an Integer that has the Number of Digits at Ten’s Place and the Least Significant Digit of the Entered Integer at One’s Place
- Python Program to Find the Sum of the Digits of the Number Recursively
- Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10