Write a Program That Takes a Number n as The Input And Prints it to The Output

In this article let's learn how to write a Python program that takes a number as a Input and then prints the number to stdout.

This program is a basic example to teach you how exactly the standard input and standard output,  the devices present in the standard input are  keyboard and mouse,  the  devices present in the standard output are monitor/ speaker.

You take the input from the standard input and then display it onto the standard output device. Let's ask the user to enter a number and then display it onto the standard output.

The number in Python can be of three times first is integer next is a fraction that is called a floating point number and another type of number is a complex number that has real and imaginary parts. Lets try to ask the user to enter all the three numbers and display it.

This can be solved using just a two line code.

 

 

Write a Program That Takes a Number n as The Input, And Prints it to The Output


Write a Python Program That Takes a Number n as The Input, And Prints it to The Output


Let's take a variable named as n,  using the input function call, ask a user to enter a number and then convert it into an integer. 

Below is the screenshot of Python execution on how you can write a Python program to ask a user to enter a number and then convert it into an integer number and then display it on to the standard output

 

Write a Program That Takes a Number n as The Input, And Prints it to The Output

 

 

I will be using the integer type conversion method by calling the function int(). This function will convert a string to an integer type  and then assigns to a variable that is present at the left hand side.

Then using the print function call print the number directly to stdout.

n = int(input("Enter a Number n : "))
print("The number you entered is : ", n)



Output
=============

Enter a Number n : 34
The number you entered is :  34



Python program for floating point number


Follow the same procedure as mentioned above by taking a variable in and instead of converting it to integer use floating point Type conversion as shown below.

n = float(input("Enter a Floating Point Number : "))
print("The number you entered is : ", n)





Output for Floating Point Variable
================================

Enter a Floating Point Number : 55.34
The number you entered is :  55.34



Python Program for complex number


Use the complex type conversion to convert the user enter input into the complex number and then print it.

n = complex(input("Enter a Complex Number : "))
print("The number you entered is : ", n)




Output for complex number
======================
Enter a Complex Number : 3+2j
The number you entered is :  (3+2j)





Conclusion
============

Execute the above Python program by dividing the input values that are integer float or complex number,  note down the results.

comment down below if you have any suggestions to improve the above  python program





Post a Comment

1 Comments