© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
Lounder

Simple Recursion [Answered]

How can I make a recursive method return false when one or more of the iterations have returned false but the top recursion has returned true?
-true
  -true
    -false // method should return false
-true
  -true
    -false // method should return false


Example with code:
        static void Main(string[] args)
        {
            /*
            8. Write a program in C# Sharp to check whether a given string is Palindrome or not using recursion. Go to the editor
            Test Data :
            Input a string : RADAR
            Expected Output :
            The string is Palindrome.
            */

            Console.WriteLine(ReverseLetters("RADAR"));
        }

        private static bool ReverseLetters(string word)
        {
            if (word.Length > 1 && word[0] == word[word.Length - 1])
            {
                ReverseLetters(word.Substring(1, word.Length - 2));
                return true;
            }

            return false;
        }
        static void Main(string[] args)
        {
            /*
            8. Write a program in C# Sharp to check whether a given string is Palindrome or not using recursion. Go to the editor
            Test Data :
            Input a string : RADAR
            Expected Output :
            The string is Palindrome.
            */

            Console.WriteLine(ReverseLetters("RADAR"));
        }

        private static bool ReverseLetters(string word)
        {
            if (word.Length > 1 && word[0] == word[word.Length - 1])
            {
                ReverseLetters(word.Substring(1, word.Length - 2));
                return true;
            }

            return false;
        }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

simple question [Answered]
C#CC# / help
4y ago
simple for loop variable [Answered]
C#CC# / help
4y ago