Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB function M-file named GCD_LCM that accepts two positive integers,

ID: 3769376 • Letter: W

Question

Write a MATLAB function M-file named GCD_LCM that accepts two positive integers, and computes their greatest common divisor and least common multiple. Example: >> [g,l]=GCD_LCM(54,24) g = 6 l = 216 Code Requirements: • You may not use MATLAB built-in functions gcd, lcm or rem. No credit will be given if these, or any similar functions, are used. Please ask if you are not sure. Input Restrictions & special cases: The function should return a red error message for the following conditions. • Any input is not positive • Any input is not an integer

Explanation / Answer

function [g,l]= GCD_LCM(x, y)
    %calculate gcd
    g = 1
    r = 0
    if(x > y)
        a = x
        b = y
    else
        a = y
        b = x
    end
  
    r = b
    while( rem(a,b) != 0)
        r = rem(a,b)
        a = b
        b = r
    end
    g = r
  
  
    % calculate lcm
    a = 1
    if(x > y)
        a = x
    else
        a = y
    end
  
    l = a
    while(true)
        if( rem(a,x) == 0 && rem(a,y) == 0)
            l =a
            break
        end
        a = a+1
    end
end

GCD_LCM(54,24)

------------output-------------------

Executing the program....
$octave -qf --no-window-system main.m

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote