Lab Cycle - 1 (Unit 1 & Unit 2)
1. (Finding or checking the numbers):Write a program for the following:
i. To Find the Largest among Three Numbers.
ii. To Find the Smallest among Three Numbers.
iii. To Check Armstrong Number.
iv. To Find Armstrong Number in an Interval.
v. To Check Prime Number.
vi. To Print all Prime Numbers in an Interval.
vii. To Find the Factorial of a Number.
viii. To Print the Fibonacci sequence.
2. (Compute the volume of a cylinder) Write a program that reads in the radius and
length of a cylinder and computes the area and volume using the following formulas:
area = radius * radius * π
volume = area * length
Here is a sample run:
3. (Financial: Compare costs): Suppose you shop for rice and find it in two
differentsizedpackages. You would like to write a program to compare the costs of
thepackages. The program prompts the user to enter the weight and price of
eachpackage and then displays the one with the better price. Here is a sample run:
4. (Sort three integers): Write a program that prompts the user to enter three integers
and displays them in increasing order.
5. (Check a number):Write a program that prompts the user to enter an integer
andchecks whether the number is divisible by both 5 and 6, divisible by 5 or 6, or
justone of them (but not both). Here is a sample run:
6. (Financial application - payroll): Write a program that reads the following
informationand prints a payroll statement:Employee’s name (e.g., Smith), Number of
hours worked in a week (e.g., 10), Hourly pay rate (e.g., 9.75), Federal tax
withholding rate (e.g., 20%), State tax withholding rate (e.g., 9%). A sample run is
shown below:
7. (Financial application - calculate interest):If you know the balance and the
annualpercentage interest rate, you can compute the interest on the next monthly
paymentusing the following formula:interest = balance * (annualInterestRate / 1200)
Write a program that reads the balance and the annual percentage interest rateand
displays the interest for the next month. Here is a sample run:
8. (Occurrence of max numbers): Write a program that reads integers, finds thelargest
of them, and counts its occurrences.
Assume that the input ends with number0. Suppose that you entered 3 5 2 5 5 5 0;
the program finds that thelargest number is 5 and the occurrence count for 5 is 4.
(Hint: Maintain two variables,max and count. The variable max stores the current
maximum number, andcount stores its occurrences. Initially, assign the first number
to max and 1 tocount. Compare each subsequent number with max. If the number is
greater thanmax, assign it to max and reset count to 1. If the number is equal to max,
incrementcount by 1.)
9. (Find the smallest factors of an integer): Write a program that reads an integer and
displaysall its smallest factors, also known as prime factors. For example, if the input
integeris 120, the output should be as follows:2, 2, 2, 3, 5.
10. (Display three patterns using loops) : Use nested loops that display the
followingpatterns in four separate programs:
11. (Print the numbers): Write aprogram to print all numbers in a range divisible by a
given number.
12. (Algebra: Solve quadratic equations):The two roots of a quadratic equation, for
example, can be obtained using the following formula: is called the discriminant of
the quadratic equation.
b2 -4ac is called the discriminant of the quadratic equation. If it is positive,
theequation has two real roots. If it is zero, the equation has one root. If it is
negative,the equation has no real roots.
Write a program that prompts the user to enter values for a, b, and c and displaysthe
result based on the discriminant. If the discriminant is positive, display tworoots. If
the discriminant is 0, display one root. Otherwise, display the equationhas no real
roots. Here are some sample runs.
13. (Display a multiplication table):Write a program that displays a multiplication table,
as shown in Figure.
14. (Calculate Digits and Characters): Write a program that accepts a string and
calculates the number of digits and letters.
15. (Simulation: heads or tails): Write a program that simulates flipping a coin one
million times and displays the number of heads and tails.
16. (Palindromic prime): A palindromic prime is a prime number that is also
[Link] example, 131 is a prime and also a palindromic prime, as are 313
and757. Write a program that displays the first 100 palindromic prime numbers.
Display10 numbers per line and align the numbers properly, as follows:
17. (Find the number of days in a month): Write a program that prompts the user to
enter the month and year and displays the number of days in the month.
For example, if the user entered month 2 and year 2000, the program should display
that February 2000 has 29 days. If the user entered month 3 and year 2005, the
program should display that March 2005 has 31 days.
18. (Reverse of the string): Write a program to reverse a given string by using the
functions. The input and output are as follows:
19. (Count upper- and lower-case letters): Write a program that accepts a string and
counts the number of upper- and lower-case letters using functions. The input and
output are as follows:
20. (Display matrix of 0s and 1s) :Write a program that displays an n-by-n matrix using
the following header: def printMatrix(n):
Each element is 0 or 1, which is generated randomly. Write a test program
thatprompts the user to enter n and displays an n-by-n matrix. Here is a sample run: