R Program to Find H.C.F. or G.C.D

bookmark

# Program to find the H.C.F of two input number

# define a function
hcf  y) {
        smaller = y
    } else {
        smaller = x
    }
    for(i in 1:smaller) {
        if((x %% i == 0) && (y %% i == 0)) {
            hcf = i
        }
    }
    return(hcf)
}

# take input from the user
num1 = as.integer(readline(prompt = "Enter first number: "))
num2 = as.integer(readline(prompt = "Enter second number: "))

print(paste("The H.C.F. of", num1,"and", num2,"is", hcf(num1, num2)))


Output

Enter first number: 72
Enter second number: 120
[1] "The H.C.F. of 72 and 120 is 24"