Question

#include <iostream> #include <string> using namespace std; int pal(int n, int temp) {    // Using...

#include <iostream>
#include <string>
using namespace std;

int pal(int n, int temp)
{
   // Using division of 10 trick
   if(n == 0)
       return temp;

   // Build the number backs.
   temp = (temp * 10) + (n % 10);

   // Then advance through the number.
   return pal(n / 10, temp);
}

int main()
{
   system("clear");

   int num;

       // Ask the user for an input.
       cout << "\nEnter any integer: ";
       cin >> num;

       int temp = pal(num, 0);

       // Pass this through to our function.
       if(temp != num)
           cout << "\nThe number is *not* a palindrome";
       else
           cout << "\nThe number *is* a palindrome";

       cout << "\n\nAgain? (1 for yes/0 for no): ";
       cin >> again;

   cout << "\n\nPress any key to continue...";
   cin.get();

}

This program lets the user enter in any number and it will check to see if it's a palindrome or not using a recursive method. What is the time complexity of this recursive method, and justify your answer. Don't just write the big O answer, give a justification in detail.

Homework Answers

Answer #1
pal takes input n and temporary variable temp for the recursion.

It calls recursively for each digit of the input variable n.
During these calls it finally calculates the reverse of the input value n and stores it into temp and finally returns the temp value.

In other words, pal is a function that calculates the reverse of the input n to temp and returns the value of temp when n becomes zero.

So, Number of recursive calls = Number of digits ib variable n.
Lets say Number of digits in n = d

So, Time complexity = O(Number of digits in n) = O(d) 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse (long       num, long   equation,long reverse = 0); int       main() {               long       num, reverse = 0;        cout << "Enter       the       num:       ";        cin >> num;        cout << "Reverse       num       is       =       "               << reverse << endl;        return       0; } long reverse(long       num, long equation, long reverse = 0) {        while (num)        {               equation...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout << "Welcome to the Calorie Count-ulator!\n"; // Get user input: cout << "Enter your meal choice ([P]izza, [S]alad, [H]amburger)\n"; cin >> meal_choice; cout << "Enter the amount of servings (1-9):\n"; cin >> servings; // TODO: Use a switch statement to evaluate the user's meal choice // Handle error checking where appropriate // Exit the program: return 0;}
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
What is my code missing? #include <iostream> #include <string> using namespace std; const double PI =...
What is my code missing? #include <iostream> #include <string> using namespace std; const double PI = 3.141592; const int LEGAL_AGE = 21; int main() {    double radius;   // input variable holds radius of circle    int age;       // input variable holds age of user    string name;       // input variable holds first name of user    // prompt user for radius    cout << "Please enter the radius of your circle: ";    cin >> radius;   ...
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main...
#include <iostream> #include <cstdlib> using namespace std; int main(int argc, char *argv[]) // declaration of main function { int limit, // a user entered limit for a loop step, // a user entered step value x; // control variable for the loop cout << "Counting up from 1 to 10\n"; // The control variable x takes on values from 1 to 10 for (x = 1; x <= 10; x++) // for loop to count from 1 to 10 values...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std;...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std; int main() {        int x;        cout << "Selection option " << endl;        cin >> x;        if (x == 1)               cout << "You select option 1" << endl;        else if (x >= 2 || x <= 4)               cout << "You select options 2 or 3 or 4" << endl;               else if (x == 10)               cout << "You select option 10" << endl;               else...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...