c++ problem maximum common multiple and minimum common multiple

Asked 2 years ago, Updated 2 years ago, 93 views

I need to find the maximum common factor and the minimum common multiple using the external class, but it can't be implemented. Please.

#include <iostream>

using namespace std;

class Test {

private:

    int input1, input2;

public:

    int a, b, c, d;
    void setSum(int a, int b);
    void GCD();
    void LCM();
};

void Test::setSum(int a, int b) {

    a = input1;
    b = input2;
}

void Test::GCD() {

    if (a < b) swap(a, b);
    while (b != 0)
    {
        c = a % b;
        a = b;
        b = a;
    }
    printf ("Maximum common multiple is %d".\n", c);
}

void swap(int &a, int &b)
{

    int temp = a;
    a = b;
    b = temp;
}

void Test::LCM() {

    d = a * b / c;
    printf ("The minimum common multiple is %d".\n", d);
}

int main(void)
{

    int a, b;
    Test A;
    cout << "Enter two numbers: ";
    cin >> a >> b;

    A.set(a , b);
    A.GCD();
    A.LCM();

    return 0;
}

class lcm

2022-09-20 19:38

1 Answers

Please refer to the code below.

#include <iostream>

using namespace std;

class Test
{
public:
    void set(int input1, int input2);
    void GCD();
    void LCM();
private:
    int num1{ 0 }, num2{ 0 }, G{ 0 }, L{ 0 };
};

void Test::set(int input1, int input2)
{
    num1 = input1;
    num2 = input2;
}

void Test::GCD()
{
    int a = num1;
    int b = num2;
    int c = 0;

    if (a < b)
    {
        int temp = a;
        a = b;
        b = temp;
    }

    while (b != 0)
    {
        c = a % b;
        a = b;
        b = c;
    }
    G = a;
    printf ("The maximum common divisor is %d".\n", G);
}

void Test::LCM()
{
    if (G != 0)
    {
        L = num1 * num2 / G;
        printf ("The minimum common multiple is %d".\n", L);
    }
}

int main()
{
    Test A;
    int a = 0, b = 0;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    A.set(a, b);
    A.GCD();
    A.LCM();

    return 0;
}


2022-09-20 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.