Loop Practice Programs
Loop Practice Programs
Loop Practice Programs
1. Write a program to find the four-digit number, call it abcd, whose digits are reversed
when the number is multiplied by 4. That is, 4 * abcd = dcba.
2. The numbers appearing on a car’s odometer range from 000000 to 999999. Write a
program to determine the number of readings that contain the digit 1.
3. Rule of 72 This rule is used to approximate the time required for prices to double due
to inflation. If the inflation rate is r%, then the Rule of 72 estimates that prices will
double in 72/r years. For instance, at an inflation rate of 6%, prices double in about
72/6 or 12 years. Write a program to test the accuracy of this rule. For each interest
rate from 1% to 20%, the program should display the rounded value of 72/r and the
actual number of years required for prices to double at an r% inflation rate. (Assume
prices increase at the end of each year.)
5. 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.
6. Write a program that prompts the user to enter an integer and checks whether the number is
divisible by both 5 and 6, divisible by 5 or 6, or just one of them (but not both)
7. Write a program that lets the user guess whether a flipped coin displays the head or the tail.
The program randomly generates an integer 0 or 1, which represents head or tail. The
program prompts the user to enter a guess and reports whether the guess is correct or
incorrect.
8. Write a program to generate a three-digit lottery number. The program prompts the user to
enter a three-digit number and determines whether the user wins according to the following
rules:
9. If the user input matches the lottery number in the exact order, the award is $10,000.
10. If all the digits in the user input match all the digits in the lottery number, the award is $3,000.
11. If one digit in the user input matches a digit in the lottery number, the award is $1,000.
12. Write a program that reads an unspecified number of integers, determines how many positive
and negative values have been read, and computes the total and average of the input values
(not counting zeros). Your program ends with the input 0. Display the average as a floating-
point number
13. Suppose that the tuition for a university is $10,000 this year and increases 5% every year.
Write a program that computes the tuition in ten years and the total cost of four years’ worth
of tuition starting ten years from now.
14. Write a program that displays, ten numbers per line, all the numbers from 100 to 200 that are
divisible by 5 or 6, but not both. The numbers are separated by exactly one space
15. Use a while loop to find the smallest integer n such that n2 is greater than 12,000.Use a while
loop to find the largest integer n such that n3 is less than 12,000.
16. Write a program to find the greatest common divisor of two integers n1 and n2 is as follows:
First find d to be the minimum of n1 and n2, and then check whether d, d - 1, d - 2, ..., 2, or 1
is a divisor for both n1 and n2 in this order. The first such common divisor is the greatest
common divisor for n1 and n2.
17. Use nested loops that display the following patterns in four separate programs:
18.
19. Write a program that lets the user enter the loan amount and loan period in number of years
and displays the monthly and total payments for each interest rate starting from 5% to 8%,
with an increment of 1/8. Here is a sample run:
20. The monthly payment for a given loan pays the principal and the interest. The monthly
interest is computed by multiplying the monthly interest rate and the balance (the remaining
principal). The principal paid for the month is therefore the monthly payment minus the
monthly interest. Write a program that lets the user enter the loan amount, number of years,
and interest rate, and then displays the amortization schedule for the loan. Here is a sample
run:
21. Write a program to sum the following series:
22.
24.
26.
27. A positive integer is called a perfect number if it is equal to the sum of all of its positive
divisors, excluding itself. For example, 6 is the first perfect number, because The next is
There are four perfect numbers less than 10,000. Write a program to find these four numbers.
28. A pentagonal number is defined asn(3n - 1)/2 forn-1.2.3.4.5…… and so on. So, the first few
numbers are 1, 5, 12, 22, .... Write a function with the following header that returns a
pentagonal number: def getPentagonalNumber(n):
29. Write a test program that uses this function to display the first 100 pentagonal numbers with
10 numbers on each line.
30. Write a function that computes the sum of the digits in an integer. Use the following function
header: def sumDigits(n):
31. A number is a palindrome if its reversal is the same as itself. Write a test program that
prompts the user to enter an integer and reports whether the integer is a palindrome. Define
reverse and ispalindromefunctions
32. Define isPrime(number) function for testing whether a number is prime. Use this function to
find the number of prime numbers less than 10,000.
33. A palindromic prime is a prime number that is also palindromic. For example, 131 is a prime
and also a palindromic prime, as are 313 and 757. Write a program that displays the first 100
palindromic prime numbers.
34. Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5, 5 and 7, and
11 and 13 are twin primes. Write a program to find all twin primes less than 1,000.