Loop Practice

  1. Write a program that reads a number of courses student takes during Academic year and the final grade for each course, and finds the average final grade for the academic year.

  2. Write a program that reads 10-digit integer, and finds and prints the average of its EVEN digits. If there are no even digits in the input, the program prints an appropriate message.

  3. Write a program that reads a sequence of non-zero integers. First zero value will terminate the input. The program finds sum and average of the negative numbers. Example: Input: 2 -5 6 7 -8 -9 0 Output: sum is -22 and average is -7.333333

  4. Write a program that reads 10 positive numbers and prints the last digit of each number.

  5. Write a program that reads a positive integer. The program finds and prints the sum of all divisors of the input number.

  6. Write a program that reads a sequence of positive integers between 100 and 999. The first integer outside of the range 100 - 999 terminates the input. The program finds the average of the last digits of the even numbers and the first digits of the odd numbers. You can assume that the input is valid and not empty - there is at least one positive integer in the input.

  7. Write a program that reads initial deposit, yearly interest, and the number of years user is planning to invest money in the bank. The program finds and prints the amount in the account after all these years.

  8. Write a program that reads deposit and interest. The program finds and prints the number of years it will take to receive $1,000,000.

  9. Write a program that reads a sequence of non-zero integers. First zero value will terminate the input. The program finds the number, the sum, and the average of the POSITIVE EVEN numbers. Write everything in main function. No additional function is required. Example: Input: 2 -6 6 7 8 -9 0 Output: there are 3 POSITIVE EVEN numbers, their sum is 16 and their average is 5.333333

  10. Write a program that reads a sequence of non-zero integers. First zero value will terminate the input. The program finds the number, the sum, and the average of the NEVATIVE ODD numbers. Write everything in main function. No additional function is required. Example: Input: 2 -6 6 -7 8 -9 0 Output: there are 2 NEGATIVE ODD numbers, their sum is -16 and their average is -8.0

  11. A positive divisor of some number K, which is different from K, is called a proper divisor of K. For example, 1, 2, and 3 are proper divisors of 6. In number theory, an abundant number is a number that is less than the sum of its proper divisors. For example, 12 is an abundant number since it is less than 1+2+3+4+6 = 16. The number 15 is NOT an abundant number since the sum of the proper divisors is 1+3+5 = 9. The number 6 is also NOT an abundant number since 1+2+3 = 6. Write a program that reads one positive integer N and prints all ODD ABUNDANT numbers between 1 and N. If the number N is ODD and ABUNDANT, the program will print N as well.

    Example 1:
    Input
    1000
    Output:
    945
    Example 2:
    Input: 2000
    Output:
    945
    1575

  12. A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers). If n is not happy, then its sequence does not go to 1. What happens instead is that it ends up in a cycle such as: 4, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89, 145, 42, 20, 4, 16, 37, 58, 89....

    For example if n = 44, we get
    Step 1: 4^2 + 4^2= 32
    Step 2: 3^2 + 2^2= 13
    Step 3: 1^2 + 3^2= 10
    Step 4: 1^2 + 0^2 = 1 - terminates the process and the output is: YES, 4

    Write a program that reads one integer and outputs the following: if the number is happy, it outputs YES and the number of steps it took to get from the input to 1 (including the step that resulted in 1), otherwise, the program outputs NO and the number of steps it took to get to the beginning of the cycle (including the step that resulted in 4)

    For example, if n = 43, we get
    Step 1: 4^2 + 3^2 = 25
    Step 2: 2^2 +5^2 = 29
    Step 3: 2^2 + 9^2 = 85
    Step 4: 8^2 + 5^2 = 89
    Step 5: 8^2 + 9^2 = 145
    Step 6: 1^2 +4^2 +5^2 = 42
    Step 7: 4^2 +2^2 = 20
    Step 8: 22 +02 = 4
    The output NO, 8