COMPUTING LCM OF TWO NUMBERS

hey guys pleassssssseeeeeeeee help...am trying to find the LCM between numbers a and b...so i made a loop of those two values multiplying by "i" as depicted from the code...the LCM of the two values is found when sum==sum2...in my text file a=5 and b=8 hence the LCM of both numbers is 40...so the loop runs if it hits 40 it shud break but its not breaking dont know y coz i used a break statement for those conditions when sum==sum2.......please can sum1 assist me..thanx in advance

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
41
42
43
44
45
46
47
 #include <iostream>
#include <vector>
#include <fstream>

using namespace std;

void getvaluesfromfile(vector<int>&A,vector<int>&B)
{
    double a=0;
    double b=0;

    ifstream infile("indata.txt");

    while(infile>>a>>b)
    {
        A.push_back(a);
        B.push_back(b);
    }
}

void calculategcd(vector<int>A,vector<int>B)
{
    int sum=0;
    int sum2=0;
    for(int i=1;i<100;i++)
    {
        sum=A[0]*i;
        sum2=B[0]*i;

        if(sum==sum2)
            break;

            cout<<sum<<"   "<<sum2<<endl;

    }

}

int main()
{
    vector<int> A;
    vector<int> B;

    getvaluesfromfile(A,B);
    calculategcd(A,B);

}
Last edited on
Are you sure that you need LCM? Your function has GCD in its name.

Your sum and sum2 will never be equal if A[0] and B[0] are not equal. Basic math:
1
2
3
X×i ? Y×i  |i∈N, X != Y  |Divide dy i
  X ? Y
  X != Y


Anyway formula for calculating LCM is following: LCM(x, y) = (|x·y|) / GCD(x,y)
thanxxxxxxxxxxxxxx a billion it works:)
Topic archived. No new replies allowed.