Write a program in java to find the hcf of two numbers.
Answers
Answered by
7
Hey There!!
Here's an HCF Program:
/* To find HCF of two numbers */
import java.io.*;
public class HCF
{
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
long a,b;
System.out.print("Enter first number: ");
a = Long.parseLong(in.readLine());
System.out.print("Enter second number: ");
b = Long.parseLong(in.readLine());
System.out.println("HCF("+a+","+b+") = "+gcd(a,b));
}
static long gcd(long a, long b)
{
while(b!=0)
{
long t = b;
b = a%b;
a = t;
}
return a;
}
}
Hope it helps
Purva
Brainly Community
Here's an HCF Program:
/* To find HCF of two numbers */
import java.io.*;
public class HCF
{
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
long a,b;
System.out.print("Enter first number: ");
a = Long.parseLong(in.readLine());
System.out.print("Enter second number: ");
b = Long.parseLong(in.readLine());
System.out.println("HCF("+a+","+b+") = "+gcd(a,b));
}
static long gcd(long a, long b)
{
while(b!=0)
{
long t = b;
b = a%b;
a = t;
}
return a;
}
}
Hope it helps
Purva
Brainly Community
YASHPRANESH:
can u plzz show it with for loops i mean in icse standard plzz
public class HCF
{
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
long a,b;
System.out.print("Enter first number: ");
a = Long.parseLong(in.readLine());
System.out.print("Enter second number: ");
b = Long.parseLong(in.readLine());
for(;b!=0;)
{
long t = b;
b = a%b;
a = t;
}
System.out.println("HCF = "+a);
}
}
Similar questions
English,
8 months ago
Hindi,
8 months ago
India Languages,
1 year ago
Math,
1 year ago
Chemistry,
1 year ago