Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Concept of G.C.D. or H.C.F & L.C.M. in programming

G.C.D. & L.C.M :

In mathematics , the greatest common divisor (gcd) of two or more integres, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.

For more detail , check the below link:



LCM means Least Common Factor that means A number that is multiple of the all the given numbers.

For more detail , check the below link:


Algorithm:

Step 1:

Take 2 inputs from the user

Step 2:

Determine the divisors of any one number

Step 3:

Check whether that number is also a divisor of another number or not

Step 4:

If yes , then store the number in a variable.

MATLAB code:

x=input('Enter the first number');
y=input('Enter the second number:');
for i=1:x
    if(rem(x,i)==0 && rem(y,i)==0)
        c=i;
    end
end
z=(x*y)/c;
disp('GCD=');
disp(c);
disp('LCM=');
disp(z);

Output:


Explanation of the Algorithm & codes in MATLAB & JAVA:



JAVA CODE:


import java.util.Scanner;

class GD

{

public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the first number:");
int x=obj.nextInt();
System.out.println("Enter the second number:");
int y=obj.nextInt();
int c=0;
for(int i=1;i<=x;i++)
{
if((x%i)==0 && (y%i==0))
{
c=i;
}
}
System.out.println("The G.C.D. is:"+c);
double z=0;
z=(x*y)/c;
System.out.println("The LCM is:"+z);
}
}

Output:

No comments

Popular Posts

Followers