Thursday, March 25, 2021

C Program palindrome Decimal and Binary (Decimal and of that Binary is palindrome testing program )

//  Decimal and of that Binary  is  palindrome testing  program 

// this is not a optimised program



#include <stdio.h>
#include <math.h>


long decimalToBinary(int dec_num)
{
    long bin_num = 0;
    int rem, temp = 1;

    while (dec_num!=0)
    {
        rem = dec_num%2;
        dec_num = dec_num / 2;
        bin_num = bin_num + rem*temp;
        temp = temp * 10;
    }
    return bin_num;
}

int palindrome_num(int decimalN){
     int n= decimalN,reversedN = 0, remainder;
     // reversed integer is stored in reversedN
    while (n != 0) {
        remainder = n % 10;
        reversedN = reversedN * 10 + remainder;
        n /= 10;
    }


    if (decimalN == reversedN){
        decimalN=1;
         
    }
    else{
        decimalN=0;
         
    }

    return decimalN;
}

int main()
{
      int n, flag_dec/*this is decimal palindrome flag */, flag_bin /*this is binary palindrome flag */;
      long int binary_num;
    printf("Enter an integer: ");
    scanf("%d", &n);
   
while(n) { //start while loop

    // check the decima number palindrome or not  
         flag_dec=palindrome_num(n);
    // convert Decinal into binary number
         binary_num=decimalToBinary(n);
     // check the binary number palindrome or not  
         flag_bin=palindrome_num(binary_num);
 
  if (flag_dec)
  if( flag_bin)
 {
        printf("\n ***********************YES*************************************");
        printf("\n Decinal numner is %ld ",n);
        printf("\n Decinal numner is %ld ",binary_num);
        printf("\n This decimal number and Binary numbera are palindrome");
        printf("\n ************************YES************************************");
     break;
     
  }
  n++;

   
}//end while loop

    return 0;
}