Time Limit Exceeded

I'm trying to get this code to run without the
time limit error. (Solo learn android app compiler) It's obviously not too complex. 1 function, the main function, 1 calculation, I looked up 'time limit errors' & found something about fprint instead of cout. But, i actually like using cout unless someone can drill in my head (nicely) that fprint is better.



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
48
49
50
#include <iostream>
using namespace std;


int LcmGcd (int num3 , int num4)
{

int lcm;
lcm=num3*num4;

while (num3!=num4)
    {
        if (num3<num4)
        {num3-=num4;}
        else 
        {num3-=num4;}
    }
    
    cout<<"gcd = "<<num3<<endl<<"lcm = "<<lcm/num3;
    
    return (0);
}



int main()
{
    int num1,num2;
    cin>>num1>>num2;
    cout<<"for("<<num1<<","<<num2<<")"<<endl;
    
    
    if(num1 == num2)
    {
            cout << "Error, can't give lcm or gcd with the same number. ";
    }
        else if(num1 != num2)
        {
            LcmGcd (num1, num2);
        }
        else
        {
            cout << "Please input two numbers. " ;
        }
        
        
     return (0);
}
    
    
Last edited on
The LcmGcd function runs into an infinite loop for some inputs.
Lines 14 and 16 are the same. I'm sure that isn't what you intended.
Topic archived. No new replies allowed.