Learn to calculate H.C.F of two number in java by ITeTrade

Learn how to calculate H.C.F of two number in java language by giving input from the keyboard or not in details with an example:

In this article, you’ll learn to find the H.C.F of two number in details with more example.

In the above diagram, 1 and 2 are the common factor of 4 and 6 respectively. But the highest common factor is 2. Therefore, H.C.F of 4 and 6 is 2.

Example 1:

import java.util.Scanner;

public class hcf {

public static void main(String[] args) {

int H;

System.out.println (“Enter two number”);

Scanner sc=new Scanner(System.in);

int a=sc.nextInt();

int b=sc.nextInt();

for(H=a<b?a:b; H>=1;H–)

if(a%H==0 && b%H==0)

break;

System.out.println(“H.C.F is “+H);

}

}

Integer variable a and b will store 4 and 6 respectively. The range of H.C.F of a and b is 1 to minimum of a 0r b.

For finding, highest common factor we create a loop from the minimum of ((a or b) to 1). For the above program

the range of H.C.F is 4 to 1 then we check a%H will give remainder 0 and b%H will give remainder zero if we get remainder zero in both cases, stop the loop and print the result.

 

Output of this program:

Enter two number

4

6

H.C.F is 2

Q: write a program in java, H.C.F of two number using while loop

Solution:

public class hcf {

public static void main(String[] args) {

int n1 =45, n2 = 15;

while(n1 != n2)

{

if(n1 > n2)

n1 -= n2;

else

n2 -= n1;

}

 

System.out.printf(“H.C.F = “+ n1);

}

}

 

Output of this program:

H.C.F = 15

Note in the above program if write negative value like int n1=45 and n2=-15 then out of this program

H.C.F = -2147483647

Which is wrong so, we modified this program to give write answer in next program which is below given.

Example:

 

public class hcf {

public static void main(String[] args) {

int n1 = 81, n2 = -15;

n1 = ( n1 > 0) ? n1 : -n1;

n2 = ( n2 > 0) ? n2 : -n2;

 

while(n1 != n2)

{

if(n1 > n2)

n1 -= n2;

else

n2 -= n1;

}

 

System.out.println(“H.C.F is ” + n1);

}

}

 

Output of this program:

H.C.F is 3

 

Example: Write the above program, with input given by user from the keyboard.

import java.util.Scanner;

 

public class hcf {

public static void main(String[] args) {

System.out.println (“Enter two number”);

Scanner sc=new Scanner(System.in);

int n1=sc.nextInt();

int n2=sc.nextInt();

n1 = ( n1 > 0) ? n1 : -n1;

n2 = ( n2 > 0) ? n2 : -n2;

 

while(n1 != n2)

{

if(n1 > n2)

n1 -= n2;

else

n2 -= n1;

}

 

System.out.println(“H.C.F is ” + n1);

}

}

 

Output of this program:

Enter two number

40

-50

H.C.F is 10

 

Q: Write a program in java that takes input from the keyboard and give L.c.m and H.c.f in a single program.

Solution:

import java.util.Scanner;

public class HcfLcm {

public static void main(String[] args) {

int a, b, x, y, t, H, L;

Scanner sc = new Scanner(System.in);

System.out.print(“Enter Two Number : “);

x = sc.nextInt();

y = sc.nextInt();

a = x;

b = y;

 

while(b != 0)

{

t = b;

b = a%b;

a = t;

}

 

H = a;

L = (x*y)/H;

 

System.out.println(“HCF = ” +H);

System.out.print(“LCM = ” +L);

}

}

 

 

Solution:

Output of this program:

Enter Two Number : 40

55

HCF = 5

LCM = 440

 

Leave a Reply

Your email address will not be published. Required fields are marked *