Showing posts with label python program. Show all posts
Showing posts with label python program. Show all posts

Thursday 27 June 2019

Python program to convert kilometers to miles Kilometer

The kilometer is a unit of length in the metric system. It is equivalent to 1000 meters.
Miles:

Mile is also the unit of length. It is equal to 1760 yards.

Conversion formula:

1 kilometer is equal to 0.62137 miles.

Miles = kilometer * 0.62137 
Kilometer = Miles / 0.62137 
See this example:

# Collect input from the user 
kilometers = float(input('How many kilometers?: ')) 
# conversion factor 
conv_fac = 0.621371 
# calculate miles 
miles = kilometers * conv_fac 
print('%0.3f kilometers is equal to %0.3f miles' %(kilometers,miles))

Tuesday 11 June 2019

Python program to check if the input number is odd or even


# A number is even if division by 2 give a remainder of 0.
# If remainder is 1, it is odd number.

num = int(input("Enter a number: "))
if (num % 2) == 0:
   print("{0} is Even".format(num))
else:
   print("{0} is Odd".format(num))

Python program to find whether the number is positive and negative

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")

Python program for simple calculator

# Program make a simple calculator that can add, subtract, multiply and divide using functions

# This function adds two numbers 
def add(x, y):
   return x + y

# This function subtracts two numbers 
def subtract(x, y):
   return x - y

# This function multiplies two numbers
def multiply(x, y):
   return x * y

# This function divides two numbers
def divide(x, y):
   return x / y

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

# Take input from the user 
choice = input("Enter choice(1/2/3/4):")

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if choice == '1':
   print(num1,"+",num2,"=", add(num1,num2))

elif choice == '2':
   print(num1,"-",num2,"=", subtract(num1,num2))

elif choice == '3':
   print(num1,"*",num2,"=", multiply(num1,num2))

elif choice == '4':
   print(num1,"/",num2,"=", divide(num1,num2))
else:
   print("Invalid input")

Python program to find the largest number among the three input numbers



# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):
   largest = num1
elif (num2 >= num1) and (num2 >= num3):
   largest = num2
else:
   largest = num3

print("The largest number between",num1,",",num2,"and",num3,"is",largest)