Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

Check if Binary Representation is Palindrome or not in C

C Program: Check if Binary Representation is Palindrome

πŸ”· C Program: Check if Binary Representation is Palindrome

#include <stdio.h>
#include <string.h>

void inttobinary(int num, char binary[])
{
    int index = 0;
    while (num > 0)
    {
        binary[index++] = (num % 2) + '0';
        num /= 2;
    }
    binary[index] = '\0';

    // Reverse the binary string
    for (int i = 0; i < index / 2; i++)
    {
        char temp = binary[i];
        binary[i] = binary[index - 1 - i];
        binary[index - 1 - i] = temp;
    }
}

int pal(char str[])
{
    int start = 0;
    int end = strlen(str) - 1;

    while (start < end)
    {
        if (str[start] != str[end])
        {
            return 0;
        }
        start++;
        end--;
    }
    return 1;
}

int main()
{
    int num;
    char binary[50];

    printf("Enter the number: ");
    scanf("%d", &num);

    if (num == 0)
    {
        printf("The binary representation of 0 is palindrome.\n");
        return 0;
    }

    inttobinary(num, binary);

    if (pal(binary))
    {
        printf("The binary representation of %d is a palindrome.\n", num);
    }
    else
    {
        printf("The binary representation of %d is not a palindrome.\n", num);
    }

    return 0;
}
  

πŸ“˜ Explanation:

This program checks whether the **binary representation** of a given decimal number is a **palindrome**.

πŸ”Ή The function inttobinary() converts a given integer to its binary equivalent using division by 2. Digits are stored in reverse order and later reversed to get the correct binary format.

πŸ”Ή The function pal() checks if a given string is a palindrome by comparing characters from both ends until the middle.

πŸ”Ή In main(), the number is taken from the user. If it is 0, it's directly reported as a binary palindrome.
πŸ”Ή Otherwise, it converts the number to binary and checks if the result is a palindrome, printing the appropriate message.

πŸ” Sample Output:

Enter the number: 9
The binary representation of 9 is a palindrome.

Enter the number: 10
The binary representation of 10 is not a palindrome.
    

🏷️ Keywords:

binary palindrome, binary conversion in C, C palindrome check, number to binary in C, C programs for beginners, string reversal, strcmp, strlen, strcpy

Comments

Popular Posts

πŸŒ™