1. Use the variables k and total to write a while loop that computes the sum of the squares of the first 50 counting numbers, and associates that value with total. Thus your code should associate 1*1 + 2*2 + 3*3 +... + 49*49 +50*50 with total. Use no variables other than k and total.
2. Given that n refers to a positive integer, use a while loop to compute the sum of the cubes of the first n counting numbers, and associate this value with total. Use no variables other than n, k, and total.
3. In this exercise, use the following variables: i, lo, hi, and result. Assume that lo and hi each are associated with an integer and that result refers to 0.
Write a while loop that adds the integers from lo up through hi (inclusive), and associates the sum with result.
Your code should not change the values associated with lo and hi. Also, just use these variables: i, lo, hi, and result.
4. Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 19, you would assign 30 to q because the perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16 and 30==1+4+9+16.
5. Assume there is a variable, h already associated with a positive integer value. Write the code necessary to count the number of perfect squares whose value is less than h, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Assign the sum you compute to a variable q. For example, if h is 19, you would assign 4 to q because there are perfect squares (starting with 1) that are less than h are: 1, 4, 9, 16.
6. Assume there are two variables, k and m, each already associated with a positive integer value and further assume that k's value is smaller than m's. Write the code necessary to compute the number of perfect squares between k and m. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the number you compute with the variable q. For example, if k and m had the values 10 and 40 respectively, you would assign 3 to q because between 10 and 40 there are these perfect squares: 16, 25, and 36,.
7. Use two variables k and total to write a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus, your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
8. Given a variable n refers to a positive integer value, use two additional variables, k and total to write a for loop to compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus your code should put 1*1*1 + 2*2*2 + 3*3*3 +... + n*n*n into total. Use no variables other than n, k, and total.
9. Associate the average of the numbers from 1 to n (where n is a positive integer value) with the variable avg.
10. Write a complete program that reads 6 numbers and assigns True to variable isAscending if the numbers are in ascending order. Otherwise assign False to it. Display the value of isAscending. Here are three sample runs:
Sample Run 1
Enter six numbers:
4
6
8
10
11
12
True
Sample Run 2
Enter six numbers:
4
6
3
10
11
12
False
Sample Run 3
Enter six numbers:
41
6
3
10
11
12
False
Note: Your program must match the sample run exactly.
11. Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1. (A perfect square is an integer like 9, 16, 25, 36 that is equal to the square of another integer (in this case 3*3, 4*4, 5*5, 6*6 respectively).) Associate the sum you compute with the variable q. For example, if h is 4, you would assign 30 to a because the first 4 perfect squares (starting with 1) are: 1, 4, 9, 16 and 30==1+4+9+16.
12. Given a positive integer n, assign True to is_prime if n has no factors other than 1 and itself. (Remember, m is a factor of n if m divides n evenly.)
13. An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. For example, in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6.
Given the positive integer distance and the positive integer n, associate the variable sum with the sum of the elements of the arithmetic progression from 1 to n with distance distance. For example, if distance is 2 and n is 10, then sum would be associated with 25 because 1+3+5+7+9 = 25.
14. Write a complete program that reads a string and displays the vowels. The vowels are A, E, I, O, U and a, e, i, o, u. Here are three sample runs:
Sample Run 1
Enter a string: Welcome to Python
The vowels are eoeoo
Sample Run 2
Enter a string: Student Union
The vowels are uelio
Sample Run 3
Enter a string: AREIQUaeiouRTE
The vowels are AEIOUaeiouE
Note: your program must match the sample run exactly.
15. Given the strings s1 and s2 that are of the same length, create a new string consisting of the first character of s1 followed by the first character of s2, followed by the second character of s1, followed by the second character of s2, and so on (in other words the new string should consist of alternating characters of s1 and s2). For example, if s1 contained "hello" and s2 contained "world", then the new string should contain "hweolrllod". Associate the new string with the variable s3.
16. Given the strings s1 and s2 that are of the same length, create a new string consisting of the last character of si followed by the last character of s2, followed by the second to last character of si, followed by the second to last character of s2, and so on (in other words the new string should consist of alternating characters of the reverse of s1 and s2). For example, if sl contained hello" and s2 contained "world", then the new string should contain "odlllreohw". Assign the new string to the variable s3.
17. Given the strings s1 and s2, not necessarily of the same length, create a new string consisting of alternating characters of s1 and s2 (that is, the first character of s1 followed by the first character of s2, followed by the second character of s1, followed by the second character of s2, and so on. Once the end of either string is reached, no additional characters are added. For example, if sl contained "abc" and s2 contained "uvwxyz", then the new string should contain "aubvcw". Assign the new string to the variable s3.
18. Given the strings sl and s2, not necessarily of the same length, create a new string consisting of alternating characters of s1 and s2 (that is, the first character of si followed by the first character of s2, followed by the second character of s1, followed by the second character of s2, and so on. Once the end of either string is reached, the remainder of the longer string is added to the end of the new string. For example, if s1 contained "abc" and s2 contained "uvwxyz", then the new string should contain "aubvcwxyz". Associate the new string with the variable s3.
19. (Count positive and negative numbers and compute the average of numbers)
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.
Sample Run 1
Enter an integer, the input ends if it is 0: 1
Enter an integer, the input ends if it is 0: 2
Enter an integer, the input ends if it is 0: -1
Enter an integer, the input ends if it is 0: 3
Enter an integer, the input ends if it is 0: 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
Sample Run 2
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
20. (Financial application: compute future tuition)
Suppose that the tuition for a university is $10,000 this year and increases 5% every year. In one year, the tuition will be $10,500. Write a program that displays the tuition in 10 years and the total cost of 4 years' worth of tuition starting after the 10th year.
21. (Find the two highest scores)
Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the student with the highest score and the student with the second-highest score. Assume that the number of students is at least 2.
Sample Run
Enter the number of students: 5
Enter a student name: Smith
Enter a student score: 60
Enter a student name: Jones
Enter a student score: 96
Enter a student name: Peterson
Enter a student score: 85
Enter a student name: Greenlaw
Enter a student score: 98
Enter a student name: Zhang
Enter a student score: 95
Top two students:
Greenlaw's score is 98.0
Jones's score is 96.0
22. (Financial application: compare loans with various interest rates)
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.
Sample Run
Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment
5.000% 188.71 11322.74
5.125% 189.29 11357.13
5.250% 189.86 11391.59
…
7.875% 202.17 12129.97
8.000% 202.76 12165.84
For the formula to compute monthly payment, see LiveExample 2.8, ComputeLoan.py.
23. (Count vowels and consonants)
Assume letters A, E, I, O, and U as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string.
Sample Run
Enter a string: Programming is fun
The number of vowels is 5
The number of consonants is 11
24. (Business: check ISBN-13)
ISBN-13 is a new standard for indentifying books. It uses 13 digits d1d2d3d4d5d6d7d8d910d11d12d13. The last digit d13 is a checksum, which is calculated from the other digits using the following formula:
10 - (d1 + 3*d2 +d3 + 3*d4 + d5 + 3*d6 + d7 + 3*d8 + d9 + 3*d10+d11 + 3*d12) % 10
If the checksum is 10, replace it with 0. Your program should read the input as a string. Display "incorrect input" if the input is incorrect.
Sample Run 1
Enter the first 12 digits of an ISBN-13 as a string: 978013213080
The ISBN-13 number is 9780132130806
Sample Run 2
Enter the first 12 digits of an ISBN-13 as a string: 978013213079
The ISBN-13 number is 9780132130790