Given an integer as input to the python script, the python script has to accept the integer and print the preceding and the succeeding integer of the given number.
You just have to write the python logic to increment and decrement the number.
Write A Python Script To Input A Number And Print Its Preceding And Succeeding Number
Let's take a integer variable named as number, using the input function call ask a user to enter a number and convert it into an integer type.
number = int(input("Enter a Number : "))
Now let's take another two variables preceding and succeeding, decrement the number by one and assign it to the proceeding variable. Then increment the number by one and assign it to the succeeding variable.
preceding = number - 1
succeeding = number + 1
Use the print function call to print the presiding and succeeding number as shown below.
print("Preceding Number is : ", preceding)
print("Succeeding Number is : ", succeeding)
Python Program
=====================
number = int(input("Enter a Number : "))
preceding = number - 1
succeeding = number + 1
print("Preceding Number is : ", preceding)
print("Succeeding Number is : ", succeeding)
Output
=================
Enter a Number : 34
Preceding Number is : 33
Succeeding Number is : 35
Output
=============
Enter a Number : 100
Preceding Number is : 99
Succeeding Number is : 101
Output
===============
Enter a Number : 50
Preceding Number is : 49
Succeeding Number is : 51
Conclusion
===================
Execute the above Python program by providing random integer value as a input to the script, note down the result.
Comment it down below if you have any suggestions to improve the above Python program.
0 Comments