Adding One Each Digits Program in Python


If a five-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example, if the number that is input is 12391 then the output should be displayed as 23402.

This program is written in the Python programming language and it is designed to accept a five-digit number from the user and then generate a new number by incrementing each digit by 1.

  • The program then initializes a variable "sum" to 0.
  • The program then uses the "//" operator to divide the "num" variable by 10000, and assigns the quotient to the variable "a". It then adds 1 to the quotient and assigns the sum to variable "a". This calculates the first digit of the new number.
  • Then program then uses the modulus operator "%" to find the remainder of the division of "num" by 10000, and assigns it to the variable "num". This is done to remove the first digit from the number and use the remaining digits in the following steps.
  • Then program then uses the "sum" variable and add the product of a and 10000 to it.
  • The program then repeats the same steps for the second digit. It uses the "//" operator to divide the "num" variable by 1000, and assigns the quotient to the variable "a". It then adds 1 to the quotient and assigns the sum to variable "a" . Then it finds the remainder of the division of "num" by 1000, and assigns it to the variable "num" , and then it adds the product of a and 1000 to the "sum" variable.
  • The program then repeats the same steps for the remaining digits. Finally, the program uses the "print()" function to display the original number and the new number generated by incrementing each digit by 1.

Source Code

num = int(input("Enter the Five Digits :"))
onum=num
sum = 0
a = (num // 10000)+1
num = num % 10000
sum=sum+(a*10000);
 
a = (num // 1000)+1
num = num % 1000
sum = sum+(a*1000)
 
a = (num // 100)+1
num = num % 100
sum = sum+(a*100)
 
 
a = (num // 10)+1
num = num % 10
sum = sum+(a*10)
 
 
a = num+1
sum = sum+a
print("Old Number :",onum)
print("New Number :",sum)
 
"""
num = int(input("Enter the Five Digits :"))
sum = 0
a = (num // 10000)+1
num = num % 10000
sum=sum+a;
 
a = (num // 1000)+1
num = num % 1000
sum = (sum*10)+a
 
a = (num // 100)+1
num = num % 100
sum = (sum*10)+a
 
 
a = (num // 10)+1
num = num % 10
sum = (sum*10)+a
 
 
a = num+1
sum = (sum*10)+a
print(sum)
"""

Output

Enter the Five Digits :43685
Old Number : 43685
New Number : 54796

Example Programs