Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,974 answers

573 users

How to find a list of numbers up to a limit that is a palindrome in base 10 and base 2 in C#

1 Answer

0 votes
using System;
using System.Text;

class PalindromeChecker
{
    // Function to check if a number is a palindrome in a given base
    static bool IsPalindrome(uint num, uint baseNum) {
        if (baseNum < 2) return false; // Invalid base

        StringBuilder strnumreversedsd = new StringBuilder();
        uint temp = num;

        // Convert number to string in given base
        do {
            uint digit = temp % baseNum;
            strnumreversedsd.Append(digit < 10 ? (char)('0' + digit) : (char)('A' + (digit - 10)));
            temp /= baseNum;
        } while (temp > 0);

        // Check palindrome
        string strnumreversed = strnumreversedsd.ToString();
        char[] reversedArray = strnumreversed.ToCharArray();
        Array.Reverse(reversedArray);

        return strnumreversed == new string(reversedArray);;
    }

    static void Main()
    {
        uint limit = 1000;

        Console.WriteLine("Numbers that are palindromes in both base 10 and base 2:");
        for (uint i = 1; i <= limit; i++) {
            if (IsPalindrome(i, 10) && IsPalindrome(i, 2)) {
                Console.Write($"{i} (binary: ");

                // Print binary representation
                StringBuilder binary = new StringBuilder();
                uint temp = i;
                do
                {
                    binary.Append((temp % 2) == 1 ? '1' : '0');
                    temp /= 2;
                } while (temp > 0);

                // Reverse binary string
                char[] binaryArray = binary.ToString().ToCharArray();
                Array.Reverse(binaryArray);
                Console.WriteLine($"{new string(binaryArray)})");
            }
        }
    }
}



/*
run:

Numbers that are palindromes in both base 10 and base 2:
1 (binary: 1)
3 (binary: 11)
5 (binary: 101)
7 (binary: 111)
9 (binary: 1001)
33 (binary: 100001)
99 (binary: 1100011)
313 (binary: 100111001)
585 (binary: 1001001001)
717 (binary: 1011001101)

*/

 



answered Nov 10, 2025 by avibootz
edited Nov 11, 2025 by avibootz
...