Write a R program to find gcd of two numbers using recursion

1. Introduction

This R program calculates the greatest common divisor (GCD) of two numbers using recursion. The GCD is the largest positive integer that divides each of the two numbers without leaving a remainder.

2. Program Steps

1. Define a function for calculating the GCD using recursion.

2. Call this function with two specific numbers.

3. Print the GCD of these numbers.

3. Code Program

# Step 1: Define a recursive function for GCD
gcd <- function(a, b) {
    if (b == 0) {
    } else {
        return(gcd(b, a %% b))

# Step 2: Call the function with two specific numbers
num1 <- 48
num2 <- 60
result <- gcd(num1, num2)

# Step 3: Print the GCD
print(paste("GCD of", num1, "and", num2, "is:"))


GCD of 48 and 60 is:


1. gcd <- function(a, b) { ... }: Defines a recursive function gcd. It uses the Euclidean algorithm for finding the GCD: if b is 0, it returns a; otherwise, it calls itself with b and a modulo b.

2. num1 <- 48, num2 <- 60: Specifies the two numbers for which the GCD is to be calculated.

3. gcd(num1, num2): Calls the gcd function with the specified numbers.

4. print(paste("GCD of", num1, "and", num2, "is:")), print(result): Prints the GCD of the two numbers.