You are on page 1of 5

1.

Write a Python Program to Convert Celsius to Fahrenheit and vice –a-


versa.
Code:
a.  convert the Celcius to Fahrenheit
#input celcius
celsius = float(input("Enter Celcius: "))

#calculate fahrenheit using the formula


fahrenheit = (9/5)*celsius + 32
print("Fahrenheit ",fahrenheit)

Output:
Enter Celcius: 48.9
Fahrenheit 120.02

b. convert Fahrenheit to Celsius.


#input fahrenheit
fahrenheit = float(input("Enter Fahrenheit: "))

celsius = (5/9)*(fahrenheit - 32)


print("Celsius ",celsius)

Output:
Enter Fahrenheit: 87.6
Celsius 30.888888888888886

2. Write a program in python to swap two variables without using


temporary variable.
Code:
# Python program to swap two variables
a = 45
b = 23
x = input('Enter value of a: ')
y = input('Enter value of b: ')
temp = a
a=b
b = temp
print('The value of a after swapping: {}'.format(a))
print('The value of b after swapping: {}'.format(b))

Output:
The value of a after swapping: 23
The value of b after swapping: 45
3. Write a Python Program to Convert Decimal to Binary, Octal and
Hexadecimal.
Code:
dec = int(input("enter integer"))
print("the decimal value of ",dec,"is:")
print(bin(dec),"in binary.")
print(oct(dec),"in octal.")
print(hex(dec),"in hexadecimal.")

Output:
enter integer : 45
the decimal value of 45 is:
binary: 0b101101
octal : 0o55
hexadecimal : 0x2d

4. Write a program to make a simple calculator (using functions).


Code:
def add(m, n):
return m + n

def subtract(m, n):


return m - n

def multiply(m, n):


return m * n

def divide(m, n):


return m / n

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

while True:

choice = input("Enter choice(1/2/3/4): ")

if choice in ('1', '2', '3', '4'):


try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue

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

elif choice == '2':


print(num1, "-", num2, "=", subtract(num1, num2))
Output:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 4
Enter first number: 63
Enter second number: 5
63.0 / 5.0 = 12.6
5. Write a program in python to find out maximum and minimum number
out of three user entered number.
Code:
num1 = int(input('Enter First number : '))
num2 = int(input('Enter Second number : '))
num3 = int(input('Enter Third number : '))
lst = [num1, num2, num3]
print("The maximum of the 3 numbers is : ", max(lst))
print("The miniumum of the 3 numbers is : ", min(lst))

Output:
Enter First number : 45
Enter Second number : 56
Enter Third number : 34
The maximum of the 3 numbers is : 56
The miniumum of the 3 numbers is : 34

6. Write a program which will allow user to enter 10 numbers and display
largest odd number from them. It will display appropriate message in case
if no odd number is found.
Code:
class LargestOdd:

def largestOdd(self, list):

currO = -1
for num in list:

num = int(num)
if(num % 2 == 1 and num > currO):

currO = num

print("Largest odd number is ", currO)

list_num = [11, 34, 54, 23, 65, 34]

obj = LargestOdd()

obj.largestOdd(list_num)

Output:
Largest odd number is 65.

7. Write a Python program to check if the number provided by the user is an


Armstrong number.
Code:

Output:
8. Write a Python program to check if the number provided by the user is a
palindrome or not.
Code:
Output:

You might also like