0% found this document useful (0 votes)
3 views

break down the recursive algorithms for both problems and then implement them in C

The document outlines recursive algorithms for counting the number of digits in a natural number and checking if a string is a palindrome. It provides C++ implementations for both algorithms, including a function to count digits by recursively dividing the number and a function to check for palindromes by comparing characters from both ends of the string. Additionally, it includes algorithm outlines and pseudocode for both problems.

Uploaded by

minhdangfw0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

break down the recursive algorithms for both problems and then implement them in C

The document outlines recursive algorithms for counting the number of digits in a natural number and checking if a string is a palindrome. It provides C++ implementations for both algorithms, including a function to count digits by recursively dividing the number and a function to check for palindromes by comparing characters from both ends of the string. Additionally, it includes algorithm outlines and pseudocode for both problems.

Uploaded by

minhdangfw0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

break down the recursive algorithms for both problems and then implement them in C++.

Recursive Algorithm to Determine the Number of Digits in a Natural Number:

1. If the number is less than 10, return 1 (since it has only one digit).
2. Otherwise, divide the number by 10 to remove the last digit and recursively call the function
with the remaining part.

3. Add 1 to the result of the recursive call.

4. The base case is when the number becomes 0.

#include <iostream>
using namespace std;

int countDigits(int n) {

if (n < 10)

return 1;

return 1 + countDigits(n / 10);

int main() {
int num = 12345;

int result = countDigits(num);


cout << "Number of digits in " << num << " is " << result << endl;

return 0;
}

Recursive Algorithm to Check if a String Is a Palindrome:

1. If the string has only one character or is empty, return true (it’s a palindrome).

2. Compare the first and last characters of the string.

3. If they are equal, recursively check the substring excluding these characters.

4. Otherwise, return false.


#include <iostream>
using namespace std;

bool isPalindrome(string str, int start, int end) {

if (start >= end)

return true;

if (str[start] != str[end])

return false;

return isPalindrome(str, start + 1, end - 1);

int main() {

string word = "malayalam";


if (isPalindrome(word, 0, word.length() - 1))

cout << "Yes, it's a palindrome." << endl;


else

cout << "No, it's not a palindrome." << endl;

return 0;

Algorithm Outline:
1. Initialize a counter to 0.

2. While the number is greater than 0:

o Divide the number by 10 (integer division).

o Increment the counter by 1.

3. The counter represents the number of digits.

Pseudocode:
function countDigits(n):
count = 0

while n > 0:
n = n // 10

count += 1

return count

Example:

• Input: n = 58964

• Function call: countDigits(n)

• Output: 5

Checking if a String Is a Palindrome


Algorithm Outline:

1. Remove spaces and convert the string to lowercase.


2. Initialize two pointers: left at the beginning and right at the end of the string.

3. While left is less than or equal to right:


o Compare characters at left and right.

o If they are different, return false.

o Otherwise, move left one step right and right one step left.

4. If the loop completes without returning false, the string is a palindrome.

Pseudocode:
function isPalindrome(s):

s = s.replace(" ", "").lower()

left, right = 0, len(s) - 1

while left <= right:

if s[left] != s[right]:

return False

left += 1

right -= 1

return True
Example:
• Input: "taco Cat"

• Function call: isPalindrome("taco Cat")


• Output: True

You might also like