repl.it
@SharkFin/

GCF Finder

C++11

Finds the greatest common factor of two numbers

fork
loading
Files
  • main.cpp
main.cpp
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
30
31
32
33
34
35
#include <iostream>
#include <cmath>

int main() {
	double num1;
	double num2;
	double tmp;

	std::cout << "First number: ";
	std::cin >> num1;
	
	std::cout << "Second number: ";
	std::cin >> num2;

	//std::cout << "One: " << num1 << " Two: " << num2 << std::endl;
	std::cout << std::endl;

	if (num1 > num2) {
		tmp = num1;
		num1 = num2;
		num2 = tmp;
	}

	double cf = 1;
	int currentFac;
	while(num1/cf >= 1) {
		if (fmod(num1 / cf, 1) + fmod(num2 / cf, 1) == 0) {
			currentFac = cf;
		}
		cf++;
	}

	std::cout << "Greatest Common Factor is " << currentFac << std::endl;
	return 0;
}
?