Search This Blog

#Day87#100DaysChallenge- Matlab Loops |GCD of two numbers

#Day87-GCD of two numbers

Task:

Write a code to Find GCD of two numbers
GCD(60,75)
15


Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab code

function num_gcf=gcfoftwonumbers(a,b)

 

if a>b

    max=a;

    min=b;

else

    max=b;

    min=a;

end

rem=mod(max,min);

if rem==0

    num_gcf=min;

else

    while rem>=1

        min_copy=rem;

        rem=mod(min,rem);

        min=min_copy;

    end

    num_gcf=min_copy;

       

   

end

 Sample Input and Output

gcfoftwonumbers(12,48)

gcfoftwonumbers(60,75)

gcfoftwonumbers(3556,3224)

gcfoftwonumbers(24,36)


ans =


    12



ans =


    15



ans =



     4



ans =


    12

 

Click here for video description




No comments

MATLAB