HCF problem - C++ basics..
I'm just getting my feet wet with C++ and could do with some pointers (no pun meant) with the logic of programming..
I'm trying to write a very basic and small program to work out the Higest Common Factor of 2 int numbers, as entered by the user.
Please don't laugh, but here is my effort, which does not work.
I'm looking for your comments on where I've gone wrong...
Code:
#include <iostream.h>
#include <iomanip.h>
int main()
{
int num1, num2, factor=0, factor1, factor2;
cout<<"Enter the first number ";
cin>>num1;
cout<<"Enter the second number ";
cin>>num2;
do
{
factor1=num1%factor;
factor2=num2%factor;
factor++;
}
while(num1%factor!=0 && num2%factor!=0);
cout<<"Factor is "<<factor;
return 0;
}
Sorry if this is a muppet question.
Hope you can 'show me the light'..
Cheers fellas (and any ladies, to be PC)
[code][/code]tagged by Salem
Just a quick query on the logic.
Okay, I think I've got the idea, however I'm a bit stuck with the following bit of code logic:
while(num1%factor || num2%factor)
I understand that this is testing the remainder of num1/factor and as long as the result is non zero, the loop continues - however, working through the logic, if the values 12 and 18 (for example) are entered and 18 is set as 'factor', when this is tested against itself the answer will be 1, as 18/18 = 1 remainder 0 - surley this should stop the loop ???? Why dosen't it??
Thanks again
Frustrated form the UK. :confused: