Unit 2 Assignment Python Programming BCC402
Unit 2 Assignment Python Programming BCC402
. Write a Python program that takes an age as input and determines whether a person is
2
eligible to vote. If the age is 18 or above, print "You are eligible to vote." Otherwise, print "You are
not eligible to vote yet.".Solution
) .
3. Write a program that prompts the user to input two integers and outputs the largest.Solution
02
. Write a program that prompts the user to enter a number and determines whether it is
4
C4
positive, negative, or zero. The program should print "Positive" if the number is greater than 0,
"Negative" if the number is less than 0, and "Zero" if the number is 0.Solution
C
. Write a program that prompts the user to enter their age and prints the corresponding age
5
(B
group. The program should use the following age groups:
-12:
0
13-19:
20-59:
Child
Teenager
Adult
g_
in
60 and above:
Senior Citizen Solution
m
6. Write a program that prompts the user to input a number from 1 to 7. The program should
display the corresponding day for the given number. For example, if the user types 1, the output
m
should be Sunday. If the user types 7, the output should be Saturday. If the number is not
between 1 to 7 user should get error message as shown in sample output.Solution
ra
. Write a program that prompts the user to enter their weight (in kilograms) and height (in
7
og
meters). The program should calculate the Body Mass Index (BMI) using the formula: BMI =
weight / (height * height). The program should then classify the BMI into one of the following
Pr
categories:
on
8. 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:
verage
A Grade
90-100
A
80-89
B
70-79
C
60-69
D
0-59
F
Solution
9. The roots of the quadratic equation ax2+ bx + c = 0, a ≠ 0 are given by the following formula:
In this formula, the term b2- 4ac is called the discriminant.If b2- 4ac = 0, then the equation has
two equal roots.
If b2- 4ac > 0, the equation has two real roots. If b2- 4ac < 0, the equation has two complex
roots.
) .
rite a program that prompts the user to input the value of a (the coefficient of x2) , b (the
W
02
coefficient of x), and c (the constant term) and outputs the roots of the quadratic equation.
Solution
C4
0. Write a program that prompts the user to enter three numbers and sorts them in ascending
1
order. The program should print the sorted numbers.Solution
C
(B
1. Write a program that prompts the user to input three integers and outputs the largest.
1
Solution
1
vowel or consonant.Solution
g_
2. Write a program that prompts the user to input a character and determine the character is
in
3. Write a program that prompts the user to input a year and determine whether the year is a
1
m
Leap Years are any year that can be evenly divided by 4. A year that is evenly divisible by 100 is a
leap year only if it is also evenly divisible by 400. Example :
ra
992
1 Leap Year
og
2000
Leap Year
1900
NOT a Leap Year
Pr
1995
NOT a Leap Year Solution
14. Write a program that prompts the user to input number of calls and calculate the monthly
telephone bills as per the following rule:
on
Plus Rs. 0.40 per call for any call beyond 200 calls.Solution
Looping Structures
[Assignment Set – 1]
1. Write a Python program to print the numbers from 1 to 10 using a for loop.Solution
2. Write a Python program to print the numbers from 20 to 1 using a while loop.Solution
.
. Write a program that prompts the user to enter a number n and prints all the numbers from 1
4
)
to n.Solution
02
. Write a program that prompts the user to enter a number n, and then prints all the odd
5
C4
numbers between 1 and n.Solution
6. Write a program that prints 'Happy Birthday!' five times on screen.Solution
C
. Write a program that takes a number n as input from the user and generates the first n terms
7
(B
of the series formed by squaring the natural numbers.
ample output
S
Enter a number: 6 g_
in
The first 6 terms of the series are:
1 4 9 16 25 36Solution
m
m
. Write a program that prompts the user to input a number and prints its multiplication table.
8
Solution
ra
. Write a Python program to print the first 8 terms of an arithmetic progression starting with 3
9
og
3 7 11 15 19 23 27 31Solution
0. Write a Python program to print the first 6 terms of a geometric sequence starting with 2 and
1
on
2 6 18 54 162 486Solution
Py
1. Write a program that asks the user for a positive integer value. The program should calculate
1
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.Solution
2. write a program that takes a positive integer N as input and calculates the sum of the
1
reciprocals of all numbers from 1 up to N. The program should display the final sum. Output of
the program should be like:
Enter a positive integer: 5
The sum of reciprocals from 1 to 5 is: 2.28Solution
3. Write a program that prompts the user to enter a number and repeats this process 5 times.
1
The program should accumulate the numbers entered and then display the final running total.
ample Output:
S
Enter a number: 10
Enter a number: 15
Enter a number: 35
Enter a number: 40
Enter a number: 50
.
The final running total is: 150Solution
)
02
4. Write a program that prompts the user to enter a positive integer and calculates its factorial.
1
The factorial of a positive integer 'n' is denoted as 'n!' and is calculated by multiplying all the
C4
integers from 1 to 'n' together. For example, the factorial of 5 (denoted as 5!) is calculated as 1 x
2 x 3 x 4 x 5.
C
he program should display the factorial value if the input is a positive number, or display a
T
(B
message stating that the factorial does not exist for negative numbers. Additionally, for an input
of zero, the program should output that the factorial of 0 is 1.Solution
1
g_
5. Write a Python program that prompts the user to enter a base number and an exponent, and
then calculates the power of the base to the exponent. The program should not use the
in
exponentiation operator (**) or the math.pow() function. The program should handle both
m
positive and negative exponents.Solution
m
ra
og
Pr
on
th
Py
Looping Structures
[AssignmentSet – 2]
. Write a Python program that prompts the user to enter a positive integer. Your
1
program should display all the factors of the number. Additionally, calculate and display
the sum of its factors.
ample output:
S
.
Enter a positive integer: 45
)
Factors: 1 3 5 9 15 45
02
Sum of factors: 78
Solution
C4
. Write a program that uses a loop to repeatedly ask the user to enter positive numbers.
2
C
The loop will come to an end when a negative number is entered. After collecting all the
positive numbers, the program will compute their sum and display the result to the user.
(B
Solution
3
g_
. Write a program that uses a loop to repeatedly ask the user to enter integers. The
loop will come to an end when zero is entered. After collecting all the integers, the
in
program will compute and display the average of all the entered numbers.
m
Solution
m
. Write a program to enter the numbers till the user wants and at the end it should
4
display the count of positive, negative and zeros entered.Solution
ra
og
. Write a program to enter the numbers till the user wants and at the end the program
5
should display the largest and smallest numbers entered.Solution
Pr
. Write a program that asks the user to input a positive integer. Your program should
6
find and display the sum of digits of number. For example, sum of digits of number
on
. An Armstrong number of three digits is an integer in which the sum of the cubes of its
7
digits is equal to the number itself. For example, 371 is an Armstrong number since 33 +
73 + 13 = 371.
. Write a program that prompts the user to input a number and reverse its digits. For
8
example, the reverse of 12345 is 54321; reverse of 5600 is 65.Solution
. A palindromic number is a number that remains the same when its digits are
9
reversed. For example, 16461. Write a program that prompts the user to input a number
and determine whether the number is palindrome or not.Solution
0. Write a program that prompts the user to input a decimal integer and display its
1
binary equivalent.Solution
1. Write a program that prompts the user to input a binary number and display its
1
decimal equivalent.Solution
2. Write a program that prompts the user to input a positive integer. It should then
1
output a message indicating whether the number is a prime number. A prime number is
a number that is evenly divisible only by itself and 1. For example, the number 5 is prime
.
because it can be evenly divided only by 1 and 5. The number 6, however, is not prime
)
02
because it can be divided evenly by I, 2, 3, and 6.Solution
C4
Fibonacci sequence the sum of two successive terms gives the third term. Following
are the first few terms of the Fibonacci sequence:
C
0 1 1 2 3 5 8 13 21 34 55 89...Solution
(B
4. Write a program that prompts the user to input two numbers and display its HCF.
1
g_
The Highest Common Factor (HCF) also called the Greatest Common Divisor (GCD) of
two whole numbers, is the largest whole number that's a factor of both of them.
in
Solution
m
15. Write a program to add first seven terms of the following series using a for loop:
m
ra
Solution
og
*********
*
* *
**********
**
*
*
**********
***
***
**********
****
****
*****
*****
4. 5. 6.
* 1
1
***
222
212
*****
33333
32123
*******
4444444
4321234
*********
555555555
543212345
Solution
18. Floyd's triangle is a right-angled triangular array of natural numbers as shown below:
) .
02
C C4
(B
Write a program to print the Floy'd triangle.Solution
1
g_
9. Write a program to compute sin x for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the computation
in
should use all terms in the series up through the term involving xn
sin x = x - x3/3! + x5/ 5! - x7/7! + x9/9! ........Solution
m
m
0. Write a program to compute cosine of x. The user should supply x and a positive
2
integer n. We compute the cosine of x using the series and the computation should use
ra
1. Write a program that generates a random number and asks the user to guess what
2
Pr
the number is. If the user's guess is higher than the random number, the program should
display "Too high, try again." If the user's guess is lower than the random number, the
on
program should display "Too low, try again." The program should use a loop that repeats
until the user correctly guesses the random number. Program should count and display
th