Professional Documents
Culture Documents
Program to convert the given temperature from Fahrenheit to Celsius and vice versa depending
upon user’s choice.
if choice == 1:
f = 9 * c / 5 + 32
elif choice == 2:
c = 5 * (f - 32) / 9
else:
print('Invalid choice.')
2.Program, to find the area of rectangle, square, circle and triangle by accepting suitable input parameters from user.
print()
5. Write a Python program to count the number of even and odd numbers from list of N numbers
list1 = [21,3,4,6,33,2,3,1,3,76]
even_count, odd_count = 0, 0
for num in list1:
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers available in the list: ", even_count)
print("Odd numbers available in the list: ", odd_count)
Output
import turtle
# set turtle
turtle.width(2)
turtle.speed(10)
def char(x):
u=0
l=0
for i in x:
if i>='a' and i<='z':
l+=1
w = ""
for i in x:
w=i+w
if (x == w):
print("Given string is palindrrome")
else:
print(“Given string is not palindrome”)
9. Write a program to find sum of all items in a dictionary.
def Sum(dic):
sum=0
for i in dic.values():
sum=sum+i
return sum
print("sum: ",Sum(dic))
10. Read a file content and copy only the contents at odd and even lines into separate new files .
print("file1 content...")
print(str1)
print("file2 content...")
print(str2)
Alg:
Program Explanation
Program:
def factorial(n):
if(n <= 1):
return 1
else:
return(n*factorial(n-1))
n = int(input("Enter number:"))
print("Factorial of a Number is:")
print(factorial(n))
12. Write a Python program for Towers of Hanoi using recursion
Alg:
Problem Description
The program prompts the user for the number of disks n and the program prints the procedure to move n disks
from peg A to peg C using peg B.
Problem Solution
1. Create function hanoi that takes the number of disks n and the names of the source, auxiliary and target pegs as
arguments.
2. The base case is when the number of disks is 1, in which case simply move the one disk from source to target
and return.
3. Move n – 1 disks from source peg to auxiliary peg using the target peg as the auxiliary.
4. Move the one remaining disk on the source to the target.
5. Move the n – 1 disks on the auxiliary peg to the target peg using the source peg as the auxiliary.
Programs: