当前位置:网站首页>Function: find the maximum common divisor and the minimum common multiple of two positive numbers

Function: find the maximum common divisor and the minimum common multiple of two positive numbers

2022-07-06 14:35:00 |Light|

requirement

Write two functions , Find the greatest common divisor and the least common multiple of two positive numbers respectively , Call these two functions with the main function and output the result . Two positive numbers are entered by keyboard .

Code

/* *  This function is used to calculate the maximum common divisor of two numbers  */
int greatest_common_divisor(int m,int n)
{
       
    int gcd=1,i;
    for(i=0;i<10;i++)
    {
    
        if(m%2==0 && n%2==0)
            {
    
                gcd = gcd*2;
                m=m/2;
                n=n/2;
                
            }
        else if(m%3==0 && n%3==0)
            {
    
                gcd = gcd*3;
                m=m/3;
                n=n/3;
    
            }
        else if(m%5==0 && n%5==0)
            {
    
                gcd = gcd*5;
                m=m/5;
                n=n/5;
                
            }
        else if(m%7==0 && n%7==0)
            {
    
                gcd = gcd*7;
                m=m/7;
                n=n/7;
            }
        else
        i=10;
    }
    return gcd;
}

/* *  This function is used to calculate the least common multiple of two numbers  */
int least_common_multiple(int m,int n)
{
    
    int lcm=0;
    lcm = m*n/greatest_common_divisor(m,n);
    return lcm;
}



test

Test input
12 4

Output
The greatest common divisor of two numbers is :4, The least common multiple is :12

原网站

版权声明
本文为[|Light|]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918543052.html