write a program to calculate HCF of two given numbers.
class nine sub - computer applications
Answers
please mark me as brainliest
Answer:
def calculate_hcf(x, y):
# selecting the smaller number
if x > y:
smaller = y
else:
smaller = x
for i in range(1,smaller + 1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
# taking input from users
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# printing the result for the users
print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num1, num2))
Answer:
Using C Programming :
#include <stdio.h>
int main()
{
int n1, n2, i, hcf;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
hcf = i;
}
printf("HCF of %d and %d is %d", n1, n2, hcf);
return 0;
}
Explanation:
In the above program, two integers entered by the user are stored in variable n1 and n2. Then, using for loop it is iterated until i is less than n1 and n2.
In each iteration of i, if both n1 and n2 are exactly divisible by i, the value of i is assigned to the variable hcf.
When the for loop is completed, the Highest common factor of two numbers is stored in variable hcf, and is displayed using printf .
Hope this answers your question.