You are on page 1of 12

1.

Program to convert the given temperature from Fahrenheit to Celsius and vice versa depending
upon user’s choice.

The given problem is solved in Python.

print('1. Celsius to Fahrenheit.')

print('2. Fahrenheit to Celsius.')

choice = int(input('Enter your choice: '))

if choice == 1:

c = float(input('Enter temperature in celsius: '))

f = 9 * c / 5 + 32

print('Temperature in fahrenheit scale:', f)

elif choice == 2:

f = float(input('Enter temperature in fahrenheit: '))

c = 5 * (f - 32) / 9

print('Temperature in celsius scale:', c)

else:

print('Invalid choice.')
2.Program, to find the area of rectangle, square, circle and triangle by accepting suitable input parameters from user.

th=int(input("Enter the height of the triangle"))


tb=int(input("Enter the breadth of the triangle"))
tarea=float((0.5)*(tb)*(th))
sl=int(input("Enter the length of the square"))
sarea=float(sl*sl)
rw=int(input("Enter the width of the rectangle"))
rh=int(input("Enter the height of the rectangle"))
rarea=float(rw*rh)
cr=int(input("Enter the radius of the circle"))
carea=float((3.14)*cr*cr)
print(" AREA OF TRIANGLE")
print("The area of Triangle is {0}",tarea)
print(" AREA OF SQUARE")
print("The area of square is {0}",sarea)
print(" AREA OF RECTANGLE")
print("The area of rectangle is {0}",rarea)
print(" AREA OF CIRCLE")
print("The area of Triangle is {0}",carea)
3. Program to calculate total marks, percentage and grade of a student. Marks obtained in each of
the five subjects are to be input by user. Assign grades according to the following criteria:
Grade A: Percentage >=80 Grade B: Percentage >=70 and <80
Grade C: Percentage >=60 and <70 Grade D: Percentage >=40 and <60
Fail: Percentage <40
Python Program to Calculate Total Marks Percentage and Grade of a Student
print("Enter the marks of five subjects::")

subject_1 = float (input ())


subject_2 = float (input ())
subject_3 = float (input ())
subject_4 = float (input ())
subject_5 = float (input ())

total, average, percentage, grade = None, None, None, None

# It will calculate the Total, Average and Percentage


total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5
average = total / 5.0
percentage = (total / 500.0) * 100

if average >= 90:


grade = 'A'
elif average >= 80 and average < 90:
grade = 'B'
elif average >= 70 and average < 80:
grade = 'C'
elif average >= 60 and average < 70:
grade = 'D'
else:
grade = 'E'

# It will produce the final output


print ("\nThe Total marks is: \t", total, "/ 500.00")
print ("\nThe Average marks is: \t", average)
print ("\nThe Percentage is: \t", percentage, "%")
print ("\nThe Grade is: \t", grade)
4. Program to display the first ‘n’ terms of Fibonacci series.

# Write a program to print fibonacci series upto n terms in python

num = int(input(“Give the n value:”))


n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
n3 = n1 + n2
n1 = n2
n2 = n3
print(n3, end=" ")

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

Even numbers available in the list: 4


Odd numbers available in the list: 6
6. Create a Turtle graphics window with specific size.

import turtle

# set turtle
turtle.width(2)
turtle.speed(10)

# loop for pattern


for i in range(10):
turtle.circle(40)
turtle.right(36)

# set screen and drawing remain as it is.


turtle.screensize(canvwidth=400, canvheight=300,
bg="blue")
7. Write a Python program using function that accepts a string and calculate the number of upper-
case letters and lower-case letters.

def char(x):
u=0
l=0
for i in x:
if i>='a' and i<='z':
l+=1

if i >='A' and i<='Z':


u+=1

print("LowerCase letter in the String",l)


print("UpperCase letter in the String",u)

x=input("Enter the string:- ")


char(x)
8. Python program to reverse a given string and check whether the give string is palindrome or
not.

x = input(“Give the string to check whether the string is palindrome or not”)

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

dic={ 'x':30, 'y':145, 'z':55 }

print("Dictionary: ", dic)

print("sum: ",Sum(dic))
10. Read a file content and copy only the contents at odd and even lines into separate new files .

# opening the file


file1 = open('file1.txt', 'r')

# creating another file to store odd lines


file2 = open('file2.txt', 'w')

# reading content of the files


# and writing odd lines to another file
lines = file1.readlines()
type(lines)
for i in range(0, len(lines)):
if(i % 2 != 0):
file2.write(lines[i])

# closing the files


file1.close()
file2.close()

# opening the files and printing their content


file1 = open('file1.txt', 'r')
file2 = open('file2.txt', 'r')

# reading and printing the files content


str1 = file1.read()
str2 = file2.read()

print("file1 content...")
print(str1)

print() # to print new line

print("file2 content...")
print(str2)

# closing the files


file1.close()
file2.close()
11. Program to find factorial of the given number using recursive function

Alg:

Program Explanation

1. The program defines a recursive function called factorial().


2. The function takes a number as input and returns the factorial of that number.
3. The base case of the recursive function is when the number is equal to 1 or 0.
4. In this case, the function returns 1.
5. Otherwise, the function returns the number multiplied by the factorial of the number minus 1.

Time Complexity: O(n)


The time complexity of the above code is O(n) because the recursive function is called n times, where n is the
input number.

Space Complexity: O(n)


The space complexity is O(n) as well because each recursive call adds a new frame to the call stack, which
requires additional space proportional to the input number.

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:

def hanoi(disks, source, auxiliary, target):


if disks == 1:
print('Move disk 1 from peg {} to peg {}.'.format(source, target))
return

hanoi(disks - 1, source, target, auxiliary)


print('Move disk {} from peg {} to peg {}.'.format(disks, source, target))
hanoi(disks - 1, auxiliary, source, target)

disks = int(input('Enter number of disks: '))


hanoi(disks, 'A', 'B', 'C')

You might also like