write a program to accept two numbers and find the HCF of the two numbers
Answers
Answer:
#include <stdio.h>
int main() {
int a, b, x, y, t, hcf;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
hcf = a;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
return 0;
}
Answer:
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
int dividend, divisor;
int remainder, hcf = 0;
System.out.print("Enter the first number ");
dividend = console.nextInt();
System.out.print("Enter the second number ");
divisor = console.nextInt();
do
{
remainder = dividend % divisor;
if(remainder == 0)
{
hcf = divisor;
}
else
{
dividend = divisor;
divisor = remainder;
}
}while(remainder != 0);
System.out.println("HCF: " + hcf);
}
}
Explanation: