-2

I'm trying to write code that will return equal dividers of a given number. I don't know where I'm wrong. Any help?

Code:

#include <iostream>
#include <stdlib.h>

using namespace std; // So the program can see cout and endl


int main()
{
    int Numerator;
    cout<<"Enter Numerator: ";
    cin>>Numerator;

    int Denominator = 1;
    while (Denominator < Numerator) {
        int divresult;
        int check;
        divresult = (Numerator / Denominator);
        check = divresult * Denominator;
        if(check = Numerator){
                cout << divresult <<endl;
        }
        Denominator++;

    }

    return 0;
}

Desired Output:

9
3
1
Mark R
  • 49
  • 6

3 Answers3

2

The line:

if(check = Numerator){

should be

if(check == Numerator){

You need == to check equality. = is for assignment. Your compiler should have given you a warning.

Also, if you want the output 1 you also need to change the line:

while (Denominator < Numerator) {

to

while (Denominator <= Numerator) {
Chris Drew
  • 14,926
  • 3
  • 34
  • 54
1

This line:

if(check = Numerator){

is wrong. Checking for equality is done with ==, while = causes the value of Numerator to be assigned to check, and then the if expression is true if the new value is true. Your compiler should have given you a warning about this mistake; if it didn't, turn on warnings. If it did, pay more attention to the warnings.

jwodder
  • 54,758
  • 12
  • 108
  • 124
1

In C++ the comparison operator is ==

if ( check == Numerator ) { ...
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29