Date: Program No.
Program: Write a program that asks the user to enter their name and their
age. Print out a message addressed to them that tells them the year that
they will turn 100 years old.
Program Code:
name=input("enter your name: ")
age=int(input("enter your age: "))
diff=100-age
year=2024+diff
print(name,"you will be 100 yrs old in year,",year)
Output:
Date: Program No. 2
Program: Write a program that converts distances measured in kilometres
to miles. One kilometre is approximately 0.62 miles.
Program Code:
dist=float(input("enter distance in kilometres: "))
miles=dist*0.62
print("distance in miles is=",miles,"miles")
Output:
Date: Program No. 3
Program: Write a program to perform a unit conversion of your own
choosing. Make sure that the program prints an introduction that explains
what it does.
Program Code:
print("Hello!! Welcome to the unit conversion program. \nEnter the distance in
kilometre and i can change it to: \n [Link] [Link] [Link] [Link]")
dist=float(input("enter distance in km: "))
ch=int(input("enter number for choice of conversion: "))
if ch==1:
print("New distance is=", dist*1000,"m")
elif ch==2:
print("New distance is=", dist*100000,"cm")
elif ch==3:
print("New distance is=", dist*0.62,"miles")
elif ch==4:
print("New distance is=", dist*3281,"feet")
else:
print("invalid")
Output:
Date: Program No. 4
Program: Write an interactive Python calculator program. The program
should allow the user to type a mathematical expression, and then print the
value of the expression. Include a loop so that the user can perform many
calculations.
Program Code:
num1=float(input("Enter the First number:- "))
num2=float(input("Enter the Second number:- "))
print("enter the operator of choice for your operation (+,-,*,/)")
ch=input ("Enter Operator:- ")
if ch=="+":
print (num1 + num2)
elif ch=="-":
print (num1 - num2)
elif ch=="*":
print (num1 * num2)
elif ch=="/":
print (num1 / num2)
else:
print ("invalid input")
Output:
Date: Program No. 6
Program: Write a program with a loop so that it executes 5 times before
quitting. Each time through the loop, the program should get another
temperature from the user and print the converted value.
Program Code:
n=5
for i in range(0,5):
temp=float(input("enter the temp in celsius: "))
print("the temp in fahrenheit is=", (temp*9/5)+32)
Output:
Date: Program No. 7
Program: Write a program to compute and print a table of Celsius
temperatures and the Fahrenheit equivalents every 10 degrees from 0°C to
100°C.
Program Code:
for i in range(0,101,10):
print("the temp in degree celsius is",i,"and the temp in fahrenheit is",
(i*9/5)+32)
Output:
Date: Program No. 8
Program: Write a program to find largest and smallest number
Program Code:
num1=float(input("enter a number: "))
num2=float(input("enter a number: "))
if num1>num2:
print(num1,"is greater than",num2)
print(num2,"is lesser than",num1)
elif num2>num1:
print(num2,"is greater than",num1)
print(num1,"is lesser than",num2)
elif num1==num2:
print(num1,"is equal to",num2)
else:
print("invalid")
Output:
Date: Program No. 9
Program: Write a program to check if a number is positive negative or zero
Program Code:
num1=float(input("enter a number: "))
if num1>0:
print(num1,"is a positive number")
elif num1<0:
print(num1,"is a negative number")
elif num1==0:
print("the number is zero")
else:
print("invalid")
Output:
Date: Program No. 10
Program: Write a program to find the factorial of a number.
Program Code:
num=int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Output:
Date: Program No. 11
Program: Write a program to count characters in a string if a character is
repeated it should count only once.
Program Code:
string="hello 112"
sumstr=0
if ([Link]("h")>=1):
sumstr+=1
if ([Link]("e")>=1):
sumstr+=1
if ([Link]("l")>=1):
sumstr+=1
if ([Link]("o")>=1):
sumstr+=1
if ([Link](" ")>=1):
sumstr+=1
if ([Link]("1")>=1):
sumstr+=1
if ([Link]("2")>=1):
sumstr+=1
print("total number of characters is=", sumstr)
Output:
Date: Program No. 12
Program: Write a program to swap two variables without using third
variables.
Program Code:
x=float(input("enter a number(x): "))
y=float(input("enter a number(y): "))
print("now to swap the variables without adding an extra variable")
x,y=y,x
print("new value of x is=",x)
print("new value of y is=",y)
Output:
Date: Program No. 13
Program: Write a program to find the sum of the first n natural numbers,
where the value of n is provided by the user.
Program Code:
n=int(input("enter the value of n: "))
sum=0
for i in range(1,n+1):
sum=sum+i
print(sum,"is the sum of the first",n,"natural numbers.")
Output:
Date: Program No. 14
Program: Write a program to find the sum of the cubes of the first n
natural numbers where the value of n is provided by the user
Program Code:
n=int(input("enter n: "))
sum=0
for i in range(1,n+1):
sum+=i**3
print(sum,"is the sum of the cubes of the 1st",n,"natural numbers")
Output:
Date: Program No. 15
Program: Write a program to print all prime numbers in an interval.
Program Code:
lower_value=int(input ("Enter the Lowest Range Value: "))
upper_value=int(input ("Enter the Upper Range Value: "))
print("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number>1:
for i in range(2, number):
if (number % i)==0:
break
else:
print (number)
Output:
Date: Program No. 16
Program: Write a program to print all odd/even numbers in a given range.
Program Code:
lower_value=int(input ("Enter the Lowest Range Value: "))
upper_value=int(input ("Enter the Upper Range Value: "))
for number in range(lower_value, upper_value + 1):
if number>0:
if number%2==0:
print(number,"is an even number")
elif number%2!=0:
print(number,"is an odd number")
else:
print(number,"is neither even nor odd")
Output:
Date: Program No. 17
Program: A Fibonacci sequence is a sequence of numbers where each
successive number is the sum of the previous two. The classic Fibonacci
sequence begins: 1, 1, 2, 3, 5, 8, 13, write a program that computes the nth
Fibonacci number where n is a value input by the user. For example, if n =
6, then the result is 8.
Program Code:
n=int(input("enter the limit: "))
x=0
y=1
z=1
print("Fibonacci Series: ")
print(x,y,end=" ")
while(z<=n):
print(z, end=" ")
x=y
y=z
z=x+y
Output:
Date: Program No. 18
Program: Write a program to calculate the volume and surface area of a
sphere from its radius, given as input.
Program Code:
r=float(input("enter the radius: "))
print("surface area of the sphere is=",4*3.14*r*r)
print("volume of the sphere is=",(4/3)*3.14*r*r*r)
Output:
Date: Program No. 19
Program: Write a program to check weather a given number is Armstrong
number
Program Code:
num=int(input("enter 3-digit number: "))
temp=num
sum=0
while(temp>0):
a=temp%10
temp=int(temp/10)
sum=sum+(a**3)
if (sum==num):
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")
Output:
Date: Program No. 20
Program: Write a program to check whether a given number is leap year
or not.
Program Code:
year=int(input("enter year: "))
if year%100==0:
if (year%400==0):
print("leap year")
elif (year%4==0):
print("leap year")
else:
print("not a leap year")
Output:
Date: Program No. 21
Program: Write a program to calculate quadratic equations.
Program Code:
import cmath
a=1
b=5
c=6
d = (b**2) - (4*a*c)
sol1 = (-[Link](d))/(2*a)
sol2 = (-b+[Link](d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Output:
Date: Program No. 22
Program: Write a menu driven Python program that should perform the following four
tasks: (i) After getting a word (of input) from the user, your program should use a while (or
for) loop to print out each of the letters of the word. Just remember that strings in Python start
with element 0. (ii) Your program should then use another loop to print out each of the letters
of the (same) word in reverse order. (ii) Make a new variable that is the original word in
reverse and print that variable. You can do this very easily in Python with a "string slice". (iv)
Ask the user for a letter to count. Use another loop to count how many times that letter
appears in the original word. Print out this count.
Program Code:
def print_letters(word):
print("Letters of the word:")
for letter in word:
print(letter)
def print_letters_reverse(word):
print("Letters of the word in reverse order:")
for letter in reversed(word):
print(letter)
def reverse_word(word):
reversed_word = word[::-1]
print("Reversed word:", reversed_word)
return reversed_word
def count_letter(word, letter_to_count):
count = 0
for letter in word:
if letter == letter_to_count:
count += 1
print(f"The letter '{letter_to_count}' appears {count} times in the word.")
def main():
while True:
print("\nMenu:")
print("1. Print each letter of the word")
print("2. Print each letter of the word in reverse order")
print("3. Print the word in reverse")
print("4. Count occurrences of a letter in the word")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '1':
word = input("Enter a word: ")
print_letters(word)
elif choice == '2':
word = input("Enter a word: ")
print_letters_reverse(word)
elif choice == '3':
word = input("Enter a word: ")
reverse_word(word)
elif choice == '4':
word = input("Enter a word: ")
letter_to_count = input("Enter the letter to count: ")
count_letter(word, letter_to_count)
elif choice == '5':
print("Exiting the program.")
break
else:
print("Invalid choice. Please choose again.")
if __name__ == "__main__":
main()
Output:
Date: Program No. 23
Program: Write a program that inputs a word and prints "Yes it starts !!"
if the given word starts with "0", "1", "2", ... , "9".
Program Code:
word=input("enter a word: ")
if word[0].isdigit():
print("Yes it starts!!")
else:
print("No, it doesn't!!")
Output:
Date: Program No. 24
Program: (i) Given a first input of 12345 and a second input of 246, what result
is produced by a Line 1? (ii) Given a first input of 12345 and a second input of
246, what result is produced by Line 2? (iii) Given a first input of 123 and a
second input of 4567, what result is produced by Line 3? (iv) Given a first input
of 123 and a second input of 4567, what result is produced by Line 4?
Program Code:
in1Str = input ("Enter string of digits: ")
in2Str = input ("Enter string of digits: ")
if len(in1Str) > len(in2Str) :
small = in2Str
large = in1Str
else :
small = in1Str
large = in2Str
newStr = ''
for element in small :
result = int(element) + int(large[0])
newStr = newStr + str(result)
large = large[1:]
print (len(newStr))#Line 1
print(newStr)#Line 2
print (large)#Line 3
print (small)#Line 4
Output:
Date: Program No. 25
Program:
Program Code:
pattern="8642"
for i in range(1, len(pattern)+1):
for j in range(i):
print(pattern[j], end="")
print()
def print_rectangle_borders(width, height):
for i in range(height):
if i == 0 or i == height - 1:
# Print the top and bottom borders
print("#" * width)
else:
# Print the sides with spaces in between
print("#" + " " * (width - 2) + "#")
width = int(input("Enter the width of the rectangle: "))
height = int(input("Enter the height of the rectangle: "))
print_rectangle_borders(width, height)
def print_z(size):
for i in range(size):
if i == 0 or i == size - 1:
# Print the top and bottom borders
print("#" * size)
else:
# Print the diagonal from top-right to bottom-left
print(" " * (size - i - 1) + "#")
size = int(input("Enter the size of the 'Z': "))
print_z(size)
Output: