Loop Practice USE LOOP FOR FOR COUNTER CONTROLLED REPETITIONS

  1. Student takes certain amount of courses during the academic year. Write a program that reads number of courses and final grade for each course, and finds the average final grade for the academic year. Use TOP-DOWN design.
  2. Write a function that accepts one parameter, which is 10-digit integer, and finds and prints the average of its EVEN digits. If there are no even digits in the parameter, the function prints an appropriate message. Write a main function to test your function
  3. Write a program that reads a sequence of non-zero integers. First zero value will terminate the input. The program finds amount, sum, and average of the negative numbers. Example: Input: 2 -5 6 7 -8 -9 0 Output: there are 3 negative numbers, their sum is -22 and their average is -7.333333
  4. Write a program that reads 10 positive three digit numbers and prints the last digits of all these numbers. You may assume that input consists of 10 positive integers between 100 and 999. YOU MUST USE LOOP FOR.
  5. Write a function sumDivisor that has one parameter positive integer. The function finds and prints the sum of all divisors of the parameter (use loop FOR in this function). Write a program that reads a sequence of positive integers. First negative or zero terminates the input. For each number, program uses function sumDivisor to find the sum of the divisors
  6. Write a program that reads a sequence of positive integers greater than 10. The first negative integer 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. Use whatever floating point format your language provides by default for the output. For example, if the user enters the positive numbers 23, 455, 668 and 24, the program will compute the average of the first digits of the odd numbers (2 and 4) and the last digits of the even numbers (8 and 4): (2 + 4 + 8 + 4) / 4 = 4.5
  7. Write a function that has 3 parameters: deposit, interest and number of years. The function finds and prints the amount in the account after all these years. Write a program that asks 5 users for information about the bank account and for each user calculates the amount of money in the account at the end of the designated period.
  8. Write a function that has 2 parameters, deposit and interest. The function finds and prints the number of years it will take to receive $1,000,000. Test your function on 5 users.
  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. (bonus question) 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