Python Program to Form an Integer that has Number of Digits at 10’s Place & LSD at 1’s Place

This is a Python Program to form an integer that has the number of digits at the ten’s place and the least significant digit in the one’s place.

Problem Description

The program takes an integer and forms a new integer which has the number of digits at the ten’s place and the least significant digit in the one’s place.

Problem Solution

1. Take in an integer and store it in a variable.
2. Make a separate copy of the integer.
3. Using a while loop, calculate the number of digits in the integer.
4. Convert the copy of the integer and the count of the number of digits to string.
5. Concatenate the last digit of the integer to the count and convert the new integer back to int.
6. Print the newly formed integer.
7. Exit.

Program/Source Code

Here is source code of the Python Program tto form an integer that has the number of digits at the ten’s place and the most significant digit in the one’s place. The program output is also shown below.

 
n=int(input("Enter the number:"))
tmp=n
k=0
while(n>0):
    k=k+1
    n=n//10
b=str(tmp)
c=str(k)
d=c+b[k-1]
print("The new number formed:",int(d))
Program Explanation

1. User must enter the number and store it in a variable.
2. A copy of the variable is made because the original value of the variable would be changed for counting the number of digits.
3. The while loop is used and the last digit of the number is obtained by using the modulus operator.
4. Each time a digit is obtained, the count value is incremented.
5. The number of digits of the variable and the number is converted to string for ease of concatenation.
6. Then the last digit of the string is obtained and concatenated to the count variable.
7. The new number formed is then printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the number:129
The new number formed: 39
 
Case 2:
Enter the number:24678
The new number formed: 58

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.