greatest common factor assignment

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.

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
36
37
38
39
40

/* 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>

using namespace 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
}

Last edited on
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
#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#Implementations
        while( a != b )
        {
            if( a > b ) a -= b ;
            else b -= a ;
        }

        std::cout << "gcd of " << first << " and " << second << " is " << a << '\n' ;
    }
}
replaced repeated subtraction by reminder
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
#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' ;
    }
}
Topic archived. No new replies allowed.