(E) Nearest factors

Goal: I will practice using control structures in C++ (i.e. if statements and loop structures) and choosing an optimal loop structure.

Instructions: This exercise requires you to retrieve the template code from directory templates/02/factors (copy it onto directory student/02/factors) and open the project in Qt Creator (in the same way as in the previous exercise).

Additional instruction

In this exercise, you can decide by yourself how many commits you will do. However, it is recommendable to do more than one of them.

Assignment

Implement into the given code template a program that divides the given product into two factors that are as near to each other as possible.

The program first asks for a positive number (product). If the input is negative or zero, the program prints ”Only positive numbers accepted”. Otherwise, it prints the product and its two factors, the smaller one of which is printed first.

Below you can see example executions of the program.

Enter a positive number: 20
20 = 4 * 5
Enter a positive number: 16
16 = 4 * 4
Enter a positive number: 5
5 = 1 * 5
Enter a positive number: 60
60 = 6 * 10
Enter a positive number: 0
Only positive numbers accepted

Tips for completing the assignment:

  • When the divisor operator (/) is applied for integers, the result gives the integer part (the part before the dot/comma in a decimal number). For example, 7 / 2 = 3.
  • Recall the meaning of the operator %.
  • Note that it is not required to divide the product into prime factors.

A+ presents the exercise submission form here.