Write a program in C which studies the number of integers wh

Write a program in C which studies the number of integers which are palindromic in base 10 and another base. Your program should go through each base, b from 2-10. It should then count how many numbers between 1 and min(1,000,000, b^10-1) are palindromic in both base 10 and base b.

Solution

// A C++ program to generate palindromic numbers

// less than n.

#include <iostream>

using namespace std;

// A utility for creating palindrome

int createPalindrome(int input, int b, bool isOdd)

{

    int n = input;

    int palin = input;

    // checks if number of digits is odd or even

    // if odd then neglect the last digit of input in

    // finding reverse as in case of odd number of

    // digits middle element occur once

    if (isOdd)

        n /= b;

    // Creates palindrome by just appending reverse

    // of number to itself

    while (n > 0)

    {

        palin = palin * b + (n % b);

        n /= b;

    }

    return palin;

}

// Fruition to print decimal palindromic number

void generatePaldindromes(int n)

{

    int number;

    // Run two times for odd and even length palindromes

    for (int j = 0; j < 2; j++)

    {

        // Creates palindrome numbers with first half as i.

        // Value of j decided whether we need an odd length

        // of even length palindrome.

        int i = 1;

        while ((number = createPalindrome(i, 10, j % 2)) < n)

        {

            cout << number << \" \";

            i++;

        }

    }

}

// Driver Program to test above function

int main()

{

    int n = 104;

    generatePaldindromes(n);

    return 0;

}

// A C++ program to generate palindromic numbers

// less than n.

#include <iostream>

using namespace std;

// A utility for creating palindrome

int createPalindrome(int input, int b, bool isOdd)

{

    int n = input;

    int palin = input;

    // checks if number of digits is odd or even

    // if odd then neglect the last digit of input in

    // finding reverse as in case of odd number of

    // digits middle element occur once

    if (isOdd)

        n /= b;

    // Creates palindrome by just appending reverse

    // of number to itself

    while (n > 0)

    {

        palin = palin * b + (n % b);

        n /= b;

    }

    return palin;

}

// Fruition to print decimal palindromic number

void generatePaldindromes(int n)

{

    int number;

    // Run two times for odd and even length palindromes

    for (int j = 0; j < 2; j++)

    {

        // Creates palindrome numbers with first half as i.

        // Value of j decided whether we need an odd length

        // of even length palindrome.

        int i = 1;

        while ((number = createPalindrome(i, 10, j % 2)) < n)

        {

            cout << number << \" \";

            i++;

        }

    }

}

// Driver Program to test above function

int main()

{

    int n = 104;

    generatePaldindromes(n);

    return 0;

}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site