w3resource

Check Palindrome number using Recursion in R

R Programming: Control Structure Exercise-19 with Solution

Write a R program function to check if a given number is a palindrome using recursion.

Sample Solution :

R Programming Code :

# Define a function to check if a given number is a palindrome using recursion
is_palindrome <- function(number) {
    # Convert the number to a character string
    num_str <- as.character(number)
    
    # Define a helper function to check if a string is a palindrome
    is_palindrome_helper <- function(str) {
        if (nchar(str) <= 1) {
            return(TRUE)  # Base case: Single character or empty string is a palindrome
        } else {
            first_char <- substr(str, 1, 1)  # Get the first character
            last_char <- substr(str, nchar(str), nchar(str))  # Get the last character
            
            # Check if the first and last characters are equal
            if (first_char != last_char) {
                return(FALSE)  # If not equal, not a palindrome
            } else {
                # Recursively check the substring without the first and last characters
                return(is_palindrome_helper(substr(str, 2, nchar(str) - 1)))
            }
        }
    }
    
    # Call the helper function with the number converted to a string
    return(is_palindrome_helper(num_str))
}

# Test the function with example inputs
number1 <- 42324
number2 <- 1234

# Check if the numbers are palindromes and print the result
cat("Number", number1, "is a palindrome!", is_palindrome(number1), "\n")
cat("Number", number2, "is a palindrome!", is_palindrome(number2), "\n")

Output:

Number 42324 is a palindrome! TRUE 
Number 1234 is a palindrome! FALSE    

Explatnaion:

In the exercise above,

  • The "is_palindrome()" function takes a number as input and checks if it's a palindrome.
  • Inside the function, the number is converted to a character string using 'as.character'.
  • A helper function "is_palindrome_helper()" is defined to check if a string is a palindrome recursively.
  • In the helper function, the base case is when the length of the string is 1 or less, in which case it returns 'TRUE'.
  • Otherwise, it checks if the first and last characters of the string are equal. If they are, it recursively checks the substring without the first and last characters.
  • The "is_palindrome()" function is called with the number converted to a string, and the result is returned.

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Print ASCII values of Lowercase letters in R.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/r-programming-exercises/control-structure/r-programming-control-structure-exercise-19.php