Python program to find HCF of two numbers.

In this python basic program, we will find the HCF of two numbers defined by users.

Steps to solve the program
  1. Take two numbers as input through the user.
  2. Using an if-else statement check which number is smaller.
  3. Use for loop with the range function to iterate over numbers from 1 to the smaller number.
  4. During iteration check whether the iterated number divides both input numbers, if yes then assign that number to the variable.
  5. After the loop is finished print the output.
				
					num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))

if num1 > num2:
    smaller = num2
else:
    smaller = num1

for i in range(1, smaller+1):
    if((num1 % i == 0) and (num2 % i == 0)):
        hcf = i 

print(f"H.C.F of {num1} and {num2}: {hcf}")
				
			

Output :

				
					Enter number 1: 150
Enter number 2: 200
H.C.F of 150 and 200: 50
				
			

find the sum of natural numbers.

Program to find LCM

Leave a Comment