C++ HCF

Exercise 1

List all factors of a number

Example:

Enter a number: 20
Factors of 20 are: 1 2 4 5 10 20
	

Base code:

#include <iostream>
using namespace std;
int main() {
	int num;
	cout << "Enter a number: ";
	cin >> num;

	// list all factors of num

}

Exercise 2

Use function to check if a number is factor of another number

Example:

Enter A: 20
Enter B: 5
5 is a factor of 20
	
Enter A: 20
Enter B: 7
7 is not a factor of 20
	

Base code:

#include <iostream>
using namespace std;
int main() {
	int num1, num2;
	cout << "Enter A: ";
	cin >> num1;
	cout << "Enter B: ";
	cin >> num2;
	
	if (isFactor(num1, num2)) {
		cout << num2 << " is a factor of " << num1 << endl;
	} else {
		cout << num2 << " is not a factor of " << num1 << endl;
	}

}

bool isFactor(int a, int b) {

	// check if b is a factor of a


}

Exercise 3

Find the highest common factor (HCF) of two numbers

Example:

Enter A: 20
Enter B: 30
HCF of 20 and 30 is 10
	
Enter A: 20
Enter B: 7
HCF of 20 and 7 is 1
	

Base code:

#include <iostream>
using namespace std;
int main() {
	int num1, num2, hcf;
	cout << "Enter A: ";
	cin >> num1;
	cout << "Enter B: ";
	cin >> num2;

	hcf = findHCF(num1, num2);
	cout << "HCF of " << num1 << " and " << num2 << " is " << hcf << endl;
}

int findHCF(int a, int b) {

	// find hcf of a and b

}