-2

Write a program to display all the proper divisors of each one of the numbers in a range of integers given by the first and last values of the range.

The proper divisors of a number n are the positive integers divisors smaller than n.

Input Two integers a, b which represent the range of the values.

Where 2 <= a < b <= 100

Output For each value in the range, the program must display the word VALUE and then the number, then in the next line the word DIVISORS followed by a blank space and then each one of the proper divisors of the number separated by a blank space, at the end there must be an end of line character.

This is what I have until now and I'm looking for a simple answer because I am a beginner enter image description here

Jason
  • 36,170
  • 5
  • 26
  • 60
starl
  • 9
  • 1
  • 3
    Post code as text not as image. See also [Efficiently getting all divisors of a given number](https://stackoverflow.com/questions/26753839/efficiently-getting-all-divisors-of-a-given-number) – Jason Nov 07 '22 at 04:40
  • 2
    Welcome to Stack Overflow. I strongly recommend taking the [tour] and reading [ask] and at least the [Asking Questions](https://stackoverflow.com/help/asking) portion of the Help Center so you have a better idea of how to use Stack Overflow. For example, never post images of text. They exclude too many people who, for one reason or another, cannot see the image. – user4581301 Nov 07 '22 at 04:41
  • To input multiple numbers you would need a loop and an array or better a [`std::vector`](https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/) to store the inputs in. Side note, give your variables readable names it makes it easier for everybody (including your future you) to understand the code. eg. `a` would becomd `divisor` and `i` would become `value`. Also stop using `using namespace std`. – Pepijn Kramer Nov 07 '22 at 04:45
  • 1
    Your code is poorly formatted - indenting it sensibly makes it easier to read. It shouldn't even be compiling - line 8 is in the wrong place for a start. – ndc85430 Nov 07 '22 at 04:57

1 Answers1

0

You can solve this problem, by first reading the requriements and then come up with a design. After the design you can start to write the code.

So, what needs to be done?

  • Read 2 values, the begin of a range and the end of a range
  • Check, if reading of this 2 values was successful and nobody enetered for example "ABC"
  • Check, if the given values are valid and form a range
  • After having a valid range, we will iterate over all values in the range using a for loop
  • As required, we will show the current value in a separate line
  • Then, we try to find all divisors for this value. For this, we use a second loop and try, if a division of the value by a potential divisor can be done without any rest
  • In that case, we found a proper divisor and output it.
  • After having shown all proper divisors on one line, we will start a new line
  • (If no valid range was given, we issue an error message)

This can be then implemented for example like the below:

#include <iostream>

int main() {

    // Inform user that he has to input 2 values
    std::cout << "\nPlease enter a range, so 2 values betwenn 2 and 100 (inclusive): ";
    
    // Read 2 values and check, if thery are valid
    int a = 0, b = 0;
    if ((std::cin >> a >> b) and (a >= 2) and (a < 100) and (b > 2) and (b <= 100) and (a < b)) {

        // Now we have read a valid range
        // Iterate over this range
        for (int value = a; value <= b; ++value) {

            // Show the value to the user
            std::cout << "VALUE " << value << '\n';

            // Now find all the divisors for this value
            for (int potentialDivisor = 1; potentialDivisor < value; ++potentialDivisor) {

                // Check, if it is a proper Divisor
                if (value % potentialDivisor == 0) {

                    // We found a divisor
                    const int properDivisor = potentialDivisor;

                    // Show the resulting divisor
                    std::cout << properDivisor << ' ';
                }
            }
            // Begin a new line for the new value
            std::cout << '\n';
        }
    }
    else
        std::cout << "\n*** Error: Invalid input values for range given\n\n";
}
A M
  • 14,694
  • 5
  • 19
  • 44