0% found this document useful (0 votes)
2 views17 pages

Program File

The document contains a series of Python programming exercises, each with a specific task such as calculating areas, checking leap years, and validating phone numbers. Each exercise includes the program code and expected output. The tasks cover various programming concepts including loops, conditionals, and data structures.

Uploaded by

indo02214
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Program File

The document contains a series of Python programming exercises, each with a specific task such as calculating areas, checking leap years, and validating phone numbers. Each exercise includes the program code and expected output. The tasks cover various programming concepts including loops, conditionals, and data structures.

Uploaded by

indo02214
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 1) Write a python program that accepts radius of a circle and prints its area.

Program:

import math

radius = 5

area = [Link] * (radius ** 2)

print(f"The area of the circle with radius {radius} is: {area:.2f}")

Output:
Question 2) Write a program to find the area of a triangle.

Program:

p, r, t = 10000, 10, 3

si = (p * r * t) / 100 ci = p * ((1 + r / 100) ** t) - p

print("Simple Interest:", si)

print("Total with SI:", p + si)

print("Compound Interest:", round(ci, 2))

print("Total with CI:", round(p + ci, 2))

Output:
Question 3) write a program to obtain x,y,z from user and calculate expression : 4x⁴+3y³+9z+6π.

Program:

import math

x = float(input("Enter value of x: "))

y = float(input("Enter value of y: "))

z = float(input("Enter value of z: "))

result = 4 * (x ** 4) + 3 * (y ** 3) + 9 * z + 6 * [Link]

print("Result =", result)

Output:
Question 4) write a program to take year as input and check if it is a leap year or not.

Program:

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

print(year, "is a Leap Year")

else:

print(year, "is not a Leap Year")

Output:
Question 5) A store charges Rs.120 per item if you buy less than 10 items. If you buy between 10 and
99 items, the cost is Rs. 100 per item. If you buy 100 or more items, the cost is Rs. 70 per item. Write
a program that asks the user how many items they are buying and prints the total cost.

Program:

items = int(input("Enter number of items: "))

if items < 10:


cost = items * 120
elif items >= 10 and items < 100:
cost = items * 100
else:
cost = items * 70

print("Total cost = Rs.", cost)

Output:
Question.6) Write a program that reads from user - (i) an hour between 1 to 12 and (ii) number of
hours ahead. The program should then print the time after those many hours, e.g.,

Program:

hour = int(input("Enter hour between 1-12: "))


ahead = int(input("How many hours ahead: "))

time = (hour + ahead) % 12

if time == 0:
time = 12

print("Time at that time would be", time, "O'clock")

Output:
Question.7) Write a program which reverses a string and stores the reversed string in a new string.

Program:

string = input("Enter a string: ")

reversed_string = string[::-1]

print("Reversed string is:", reversed_string)

Output:
Question.8 ) Write a program that prompts for a phone number of 10 digits and two dashes, with
dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal input.

Program:

phone = input("Enter phone number (XXX-XXX-XXXX): ")


if len(phone) == 12 and phone[3] == '-' and phone[7] == '-':
part1 = phone[0:3]
part2 = phone[4:7]
part3 = phone[8:12]
if [Link]() and [Link]() and [Link]():
print("Phone number format is valid")
else:
print("Phone number is not valid")
else:
print("Invalid phone number format")

Output:
Question.9) Write a program to check if a number is present in the list or not. If the number is
present, print the position of the number. Print an appropriate message if the number is not present
in the list.

Program:

numbers = [10, 20, 30, 40, 50]

num = int(input("Enter a number to search: "))

if num in numbers:
position = [Link](num)
print("Number found at position:", position)
else:
print("Number is not present in the list")

Output:
Question .10) Write a program rotates the elements of a list so that the element at the first index
moves to the second index, the element in the second index moves to the third index, etc., and the
element in the last index moves to the first index.

Program:

numbers = [1, 2, 3, 4, 5]

last = numbers[-1]

for i in range(len(numbers)-1, 0, -1):


numbers[i] = numbers[i-1]

numbers[0] = last

print("Rotated list:", numbers)

Output:
Question.11) Write a program to input numbers from the user. Store these numbers in a tuple. Print
the maximum and minimum number from this tuple.

Program:

numbers = input("Enter numbers separated by space: ")

num_tuple = tuple(map(int, [Link]()))

print("Tuple:", num_tuple)
print("Maximum number:", max(num_tuple))
print("Minimum number:", min(num_tuple))

Output:
Question.12) Write a program that inputs two tuples and creates a third, that contains all elements
of the first followed by all elements of the second.

Program:

tuple1 = tuple(input("Enter elements of first tuple separated by space: ").split())

tuple2 = tuple(input("Enter elements of second tuple separated by space: ").split())

tuple3 = tuple1 + tuple2

print("Third tuple is:", tuple3)

Output:
Question .13 )Write a program to enter names of employees and their salaries as input and store
them in a dictionary.

Program:

employees = {}

n = int(input("Enter number of employees: "))

for i in range(n):

name = input("Enter employee name: ")

salary = int(input("Enter salary: "))

employees[name] = salary

print("Employee Dictionary:")

print(employees)

Output:
Question.14) Write a program to convert a number entered by the user into its corresponding
number in words. For example, if the input is 876 then the output should be 'Eight Seven Six'.

Program:

digits = {
'0': 'Zero',
'1': 'One',
'2': 'Two',
'3': 'Three',
'4': 'Four',
'5': 'Five',
'6': 'Six',
'7': 'Seven',
'8': 'Eight',
'9': 'Nine'
}

number = input("Enter a number: ")

for digit in number:


print(digits[digit], end=" ")

Output:
Question .15) Write a program to generate 6 random numbers and then print their mean, median
and mode.

Program:

import random
import statistics
numbers = []
for i in range(6):
[Link]([Link](1, 100))
print("Random Numbers:", numbers)
print("Mean =", [Link](numbers))
print("Median =", [Link](numbers))
print("Mode =", [Link](numbers))

Output:
Question .16) Write a program to calculate the area of an equilateral triangle. (area =
√3/2*side*side).

Program:

import math
side = float(input("Enter the side of the triangle: "))
area = ([Link](3) / 2) * side * side
print("Area of equilateral triangle =", area)

Output:

You might also like