Question on loops

Okay so I've already finished my code, but was wondering about a certain problem that I'm sure will come up later in my class. What if I wanted my code to loop, but not ask the user if they want to run it again?

So instead of asking, "Would you like to find out another LCM and GCF?"
The code would instead just ask;
"Enter value a: "
"Enter value b: "
Then give the values,
Then again ask
"Enter value a: "
"Enter value b: "

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

int main()
{
	int  a, b;
	int GCF, LCM;

	cout << "Enter value a: ";
	cin >> a;
	cout << "Enter value b: ";
	cin >> b;


	for (int i = a; i >= 1; i--)
	{
		if (a % i == 0 && b % i == 0)
		{
			GCF = i;
			break;
		}
	}

	LCM = a * b / GCF;
	cout << "The LCM is :" << LCM << endl
	     << "The GCF is :" << GCF << endl;

return 0;
}
Last edited on
Just wrap your code in an infinite loop: while(true)
@LB

Thanks!
Topic archived. No new replies allowed.