Python Assignment By Atul Sadiwal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

BCA IV Semester

Assignment - I (Python)

Question 1
Write a program that asks the user for his name and then welcomes
him. The output should look like this :
Enter your name : Saksham
Hello Saksham
Program
name = input("Enter your name: ")
print("Hello", name)
Output
Enter your name: Saksham
Hello Saksham

Question 2
Write a program that prompts the user to enter two integers and
display the total on the screen.
Program
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
total = num1 + num2
print("The total is:", total)
Output
Enter the first integer: 3
Enter the second integer: 4
The total is: 7

Question 3
Write a program that prompts the user to input a Celsius
temperature and outputs the equivalent temperature in Fahrenheit.
Program
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit: ", fahrenheit)
Output
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.0

Question 4
Write a program which accept principle, rate and time from user
and print the simple interest.
Program
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time period (in years): "))
simple_interest = (principal * rate * time) / 100
print("Simple interest = ", simple_interest)
Output
Enter the principal amount: 1000
Enter the rate of interest: 5
Enter the time period (in years): 2
Simple interest = 100.0

Question 5
Write a program that accepts seconds from keyboard as integer.
Your program should convert seconds in hours, minutes, and
seconds. Your output should like this :
Enter seconds: 13400
Hours: 3
Minutes: 43
Seconds: 20
Program
seconds = int(input("Enter seconds: "))
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
print("Hours:", hours)
print("Minutes:", minutes)
print("Seconds:", seconds)
Output
Enter seconds: 13400
Hours: 3
Minutes: 43
Seconds: 20

Question 6
Write a program that prompts the user to enter number in two
variables and swap the contents of the variables.
Program
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
temp = num1
num1 = num2
num2 = temp
print("After swapping:")
print("num1 =", num1)
print("num2 =", num2)
Output
Enter the first number: 3
Enter the second number: 5
After swapping:
num1 = 5.0
num2 = 3.0

Question 7
Write a program that prompts the user to enter number in two
variables and swap the contents of the variables.(Do not declare
extra variable.)
Program
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num1, num2 = num2, num1
print("After swapping:")
print("num1 =", num1)
print("num2 =", num2)
Output
Enter the first number: 3
Enter the second number: 5
After swapping:
num1 = 5.0
num2 = 3.0

Question 8
Write a program that prompts the user to input the radius of a
circle and outputs the area and circumference of the circle.
Program
radius = float(input("Enter the radius of the circle: "))
pi = 3.14159
area = pi * radius ** 2
circumference = 2 * pi * radius
print("The area of the circle is:", area)
print("The circumference of the circle is:", circumference)
Output
Enter the radius of the circle: 5
The area of the circle is: 78.53975
The circumference of the circle is: 31.4159

Question 9
Write a program that prompts the user to input the length and the
width of a rectangle and outputs the area and circumference of
the rectangle.
Program
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
circumference = 2 * (length + width)
print("The area of the rectangle is:", area)
print("The circumference of the rectangle is:", circumference)
Output
Enter the length of the rectangle: 6
Enter the width of the rectangle: 4
The area of the rectangle is: 24.0
The circumference of the rectangle is: 20.0

Question 10
Write a program that prompts the user to input a number and display
if the number is even or odd.
Program
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
Output
Enter a number: 7
7 is odd

Question 11
Write a program that prompts the user to input two integers and
outputs the largest.
Program
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 > num2:
print(num1, "is the largest")
else:
print(num2, "is the largest")
Output
Enter the first number: 5
Enter the second number: 9
9 is the largest

Question 12
Write a program that prompts the user to input three integers and
outputs the largest.
Program
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
num3 = int(input("Enter the third number: "))
if num1 >= num2 and num1 >= num3:
print(num1, "is the largest")
elif num2 >= num1 and num2 >= num3:
print(num2, "is the largest")
else:
print(num3, "is the largest")
Output
Enter the first number: 5
Enter the second number: 9
Enter the third number: 3
9 is the largest

Question 13
Write a program that prompts the user to input a year and determine
whether the year 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
Enter a year: 2024
2024 is a leap year

Question 14
Write a program that prompts the user to input number of calls
and calculate the monthly telephone bills as per the following
rule:
Minimum Rs. 200 for up to 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls.
Program
calls = int(input("Enter the number of calls: "))
if calls <= 100:
bill = 200
elif calls <= 150:
bill = 200 + 0.60 * (calls - 100)
elif calls <= 200:
bill = 200 + 0.60 * 50 + 0.50 * (calls - 150)
else:
bill = 200 + 0.60 * 50 + 0.50 * 50 + 0.40 * (calls - 200)
print("Your monthly telephone bill is Rs.", bill)
Output
Enter the number of calls: 175
Your monthly telephone bill is Rs. 235.0

Question 15
The marks obtained by a student in 3 different subjects are input
by the user. Your program should calculate the average of subjects
and display the grade. The student gets a grade as per the
following rules:
Average Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Program
subject1 = float(input("Enter marks obtained in subject 1: "))
subject2 = float(input("Enter marks obtained in subject 2: "))
subject3 = float(input("Enter marks obtained in subject 3: "))
average = (subject1 + subject2 + subject3) / 3
if average >= 90:
grade = "A"
elif average >= 80:
grade = "B"
elif average >= 70:
grade = "C"
elif average >= 60:
grade = "D"
else:
grade = "F"
print("Your average marks are:", average)
print("Your grade is:", grade)
Output
Enter marks obtained in subject 1: 85
Enter marks obtained in subject 2: 92
Enter marks obtained in subject 3: 78
Your average marks are: 85.0
Your grade is: B

Question 16
Write a program that prompts the user to input a number. Program
should display the corresponding days to the number.
Program
number = int(input("Enter a number between 1 and 7: "))
if number == 1:
day = "Monday"
elif number == 2:
day = "Tuesday"
elif number == 3:
day = "Wednesday"
elif number == 4:
day = "Thursday"
elif number == 5:
day = "Friday"
elif number == 6:
day = "Saturday"
elif number == 7:
day = "Sunday"
else:
day = "Invalid input"
print("The corresponding day is:", day)
Output
Enter a number between 1 and 7: 3
The corresponding day is: Wednesday

Question 17
Write a program that prompts the user to input a character and
determine the character is vowel or consonant.
Program
char = input("Enter a character: ")
if char in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']:
print(char, "is a vowel")
else:
print(char, "is a consonant")
Output 1
Enter a character: a
a is a vowel
Output 2
Enter a character: b
b is a consonant
Question 18
Write a program to print numbers from 1 to 10.
Program
for i in range(1, 11):
print(i)
Output
1
2
3
4
5
6
7
8
9
10

Question 19
Write a program that asks the user for a positive integer value.
The program should calculate the sum of all the integers from 1
up to the number entered. For example, if the user enters 20, the
loop will find the sum of 1, 2, 3, 4, ... 20.
Program
n = int(input("Enter a positive integer: "))
total = 0
for i in range(1, n+1):
total += i
print("The sum of all integers from 1 to", n, "is", total)
Output
Enter a positive integer: 20
The sum of all integers from 1 to 20 is 210

Question 20
Write a program that prompts the user to input a number and prints
its multiplication table.
Program
num = int(input("Enter a number: "))
print("Multiplication table of", num)
for i in range(1, 11):
print(num, "x", i, "=", num * i)
Output
Enter a number: 7
Multiplication table of 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Question 21
Write a program that prompts the user to input a number and prints
its factorial.
Program
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num+1):
factorial *= i
print("The factorial of", num, "is", factorial)
Output
Enter a number: 5
The factorial of 5 is 120

Question 22
Two numbers are entered through the keyboard. Write a program to
find the value of one number raised to the power of another.
Program
num1 = int(input("Enter the base number: "))
num2 = int(input("Enter the exponent: "))
result = num1 ** num2
print(num1, "raised to the power of", num2, "is", result)
Output
Enter the base number: 2
Enter the exponent: 4
2 raised to the power of 4 is 16

Question 23
Write a program that prompts the user to input a number and reverse
its digits.
Program
number = int(input("Enter a number: "))
reverse = 0
original = number
while number > 0:
digit = number % 10
reverse = (reverse * 10) + digit
number = number // 10
print("The reverse of", original, "is", reverse)
Output
Enter a number: 12345
The reverse of 12345 is 54321

Question 24
Write a program that asks the user to input a positive integer.
Your program should find and display the sum of digits of number.
Program
num = int(input("Enter a positive integer: "))
sum = 0
while num > 0:
digit = num % 10
sum += digit
num //= 10
print("The sum of digits of the number is:", sum)
Output
Enter a positive integer: 12345
The sum of digits of the number is: 15

Question 25
Write a program that prompts the user to input a decimal integer
and display its binary equivalent.
Program
decimal = int(input("Enter a decimal integer: "))
binary = bin(decimal)
print(f"The binary equivalent of {decimal} is {binary}.")
Output
Enter a decimal integer: 23
The binary equivalent of 23 is 0b10111.
Question 26
Write a program that prompts the user to input two numbers and
display its HCF.
Program
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
smaller = num2
else:
smaller = num1
for i in range(1, smaller+1):
if((num1 % i == 0) and (num2 % i == 0)):
hcf = i
print("The HCF of", num1, "and", num2, "is", hcf)
Output
Enter first number: 24
Enter second number: 36
The HCF of 24 and 36 is 12

Question 27
Write a program to find all Armstrong number in the range of 0
and 999
Program
for num in range(0, 1000):
sum = 0
order = len(str(num))
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
Output
0
1
2
3
4
5
6
7
8
9
153
370
371
407

Question 28
Write programs to print following patterns :
1.

**********
**********
**********
**********

Program
for i in range(4):
print("*" * 10)
2.

*
**
***
****
*****
Program
for i in range(1, 6):
for j in range(i):
print("*", end="")
print()

3.

*
**
***
****
*****
Program
n = 5
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for k in range(i+1):
print("* ", end="")
print()

4.

*
***
*****
*******
*********
Program
for i in range(1, 6):
print(" " * (5-i) + "*" * (2*i-1))

5.

1
222
33333
4444444
555555555
Program
for i in range(1, 6):
print(" " * (5-i) + str(i) * (2*i-1))

Question 29
Write a program that accepts a list from user and print the
alternate element of list.
Program
lst = input("Enter a list of elements separated by spaces:
").split()
print("Alternate elements of the list:")
for i in range(0, len(lst), 2):
print(lst[i])
Output
Enter a list of elements separated by spaces: 1 2 3 4 5 6
Alternate elements of the list:
1
3
5
Question 30
Write a program that accepts a list from user. Your program should
reverse the content of list and display it. Do not use reverse()
Program
lst = input("Enter a list of elements separated by spaces:
").split()
reversed_lst = []
for i in range(len(lst)-1, -1, -1):
reversed_lst.append(lst[i])
print("Reversed list:")
for item in reversed_lst:
print(item, end=" ")
Output
Enter a list of elements separated by spaces: 1 2 3 4 5
Reversed list: 5 4 3 2 1

Made By :-- Atul.Xt

You might also like