I need help with following algorithm:
Initaliaze a counter at 2;
So long as the counter is less then or equal to number;
if the counter divides the number evenly; //<<==problem
display the counter;
else add one to the counter;
-------
so i just cant figure how to check: "if the counter divides the number evenly;"
that's my code

intNumb = 0;
    cout << "Enter a number: ";
    cin >> intNumb;
    intDivider = 2;
    
    while (intDivider <= intNumb) {
          
          if ( intNumb%intDivider!= intNumb ) //<<==problem
           {
           cout << intDivider<<" ";
           intNumb = intNumb / intDivider;
           }
           else
           {
               
               intDivider++;
               }
          
          }

Thank you.

Recommended Answers

All 3 Replies

so i just cant figure how to check: "if the counter divides the number evenly;"

Something like

if ( intNumb % intDivider == 0 &&  (intNumb/intDivider)%2 == 0)
{
          // intNumb is divided by intDivider evenly
}

I hope I got the meaning of dividing evenly correctly.

Don't need the compound if. if ((intNumb % intDivider) == 0) will suffice.

Don't need the compound if. if ((intNumb % intDivider) == 0) will suffice.

Looks like I interpreted the meaning of dividing evenly incorrectly.

Say for example intNumb = 6 and intDivider = 2. Clearly intNumb is divisible by intDivider, so intNumb % intDivider == 0 is true. But the quotient is 3, which is odd. So I thought intNumb is not divided evenly by intDivider. Hence the second test.

Did some searching, and WaltP is correct. So no need for the second part of the comparison.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.