Python program to convert Centimeter into Inches

Python program to convert Centimeter into Inches

In this article, We will see How to Convert the Centimeter unit into Inches.
Example:

Input: Centimeter: 8
Output: Inches: 3.152 

As we know that, 1 centimeter = 0.394 inches (approx) so we are using this information for writing this program.

Now, let’s see the different ways to code this program:
Code 1:

# given input
centimeter = 8

# conversion factor mention
conversion_factor = 0.394

# conversion from centimeter to inches
inches = centimeter * conversion_factor

# print
print("Centimeter Units:", centimeter)
print("One Centimeter is equal to", conversion_factor, "inches")
print("So",centimeter,"Centimeter is equal to:", inches,"inches")

Output:

Centimeter Units: 8
One Centimeter is equal to 0.394 inches
So 8 Centimeter is equal to: 3.152 inches

Code 2: By creating user-defined function.

# Create a user defined function for
# conversion from centimeter to inches
def centi_to_inches(centimeter) :
    
    # conversion factor mention
    conversion_factor = 0.394

    # conversion from centimeter to inches
    inches = centimeter * conversion_factor

    # print
    print("Centimeter Units:", centimeter)
    print("One Centimeter is equal to", conversion_factor, "inches")
    print("So",centimeter,"Centimeter is equal to:", inches,"inches")

# Main code
if __name__ == "__main__" :

    # given input
    centimeter = 8

    # function calling
    centi_to_inches(centimeter)

Output:

Centimeter Units: 8
One Centimeter is equal to 0.394 inches
So 8 Centimeter is equal to: 3.152 inches

Code 3: By Creating anonymous : one- line function

# Create a anonymous/lambda function for
# conversion from centimeter to inches
convert = lambda centi : centi * 0.394

# Main code
if __name__ == "__main__" :

    # given input
    centimeter = 8

    # print
    print("Centimeter Units:", centimeter)

    # function calling and printing
    print(centimeter,"Centimeter is equal to:", convert(centimeter),"inches")

Output:

Centimeter Units: 8
8 Centimeter is equal to: 3.152 inches

Code 4: In this code, we are taking input from user and convert it into integer and then pass it into convert anonymous function.

# Create a anonymous/lambda function for
# conversion from centimeter to inches
convert = lambda centi : centi * 0.394

# Main code
if __name__ == "__main__" :

    # take input from user and
    # convert it into integer data type
    centimeter = int(input("Enter centimeter units:"))

    # print
    print("Centimeter Units:", centimeter)

    # function calling and printing
    print(centimeter,"Centimeter is equal to:", convert(centimeter),"inches")

Output:

Enter centimeter units:10
Centimeter Units: 10
10 Centimeter is equal to: 3.9400000000000004 inches

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds