I need help with my lab. The point of it is to make a program that finds the greatest common factor of two numbers. We don't need to worry about negatives or zeros. I literally have no idea what I am doing. Thank you.
/* GreatestCommonFactor.cpp
Written by xxxxxxxxxxxxxx
Date: 03/06/2019
Use an algorithm to compute the greatest common factor between two numbers.
*/
#include <iostream>
#include <stdlib.h>
usingnamespace std;
int main()
{
int numer, denom; // declare variables numer and denom as ints
int gcf = 1; // initialize gcf variable to 1
int k = 2; // initialize k (counter variable) to 2
cout << "This program calculates the GCF\n\n";
cout << "Input the 1st value: ";
cin >> numer; // 1st input value from keyboard
cout << "Input the 2nd value: ";
cin >> denom; // 2nd input value from keyboard
// loop while k is less than or equal to numer and k is less than or equal to denom
{
// if both are evenly divisible by k?
// assign k to the gcf
// add 1 to k
} // end of looping
cout << "\nThe Greatest Common Factor of "
<< numer << " and " << denom << " is " << gcf // output the answer
<< endl << endl;
system("PAUSE"); // pause the answer so we can read it
return 0; // return control to the OS
}
#include <iostream>
int main()
{
std::cout << "enter two positive integers: " ;
int first, second ;
std::cin >> first >> second ;
if( first > 0 && second > 0 ) // if both are positive
{
int a = first ;
int b = second ;
// Euclidean algorithm : https://en.wikipedia.org/wiki/Euclidean_algorithm
// Eucid's original version (based on repeated subtraction)
// see: https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementationswhile( a != b )
{
if( a > b ) a -= b ;
else b -= a ;
}
std::cout << "gcd of " << first << " and " << second << " is " << a << '\n' ;
}
}
#include <iostream>
int main()
{
std::cout << "enter two positive integers: " ;
int first, second, h;
std::cin >> first >> second ;
if( first > 0 && second > 0 ) // if both are positive
{
int a = first ;
int b = second ;
// Euclidean algorithm : https://en.wikipedia.org/wiki/Euclidean_algorithm
// Eucid's original version (based on repeated subtraction)
// see: https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
/*
** replaced repeated subtraction by reminder
** gain hardly measurable with compiled C++, no need to change anything
*/
while( a != 0 )
{
h = a;
a = b % a;
b = h;
}
std::cout << "GCD of " << first << " and " << second << " is " << b << '\n' ;
std::cout << "LCM of " << first << " and " << second << " is " << first * second / b << '\n' ;
}
}