C Program To Convert Decimal into Binary, Hexadecimal and Octal - IProgramX

Q. Write a program to accept a decimal number and convert it to binary, octal and hexadecimal. Write separate functions


Program

#include<stdio.h>
#include<stdlib.h>
int main()
{
      int num, choice;
      printf("\nEnter Decimal Number:\t");
      scanf("%d", &num);
                printf("\nBinary Value     :\t");
                conversion(num, 2);
                printf("\nOctal Value      :\t");
                conversion(num, 8);
printf("\nHexadecimal Value:\t");
                conversion(num, 16);
      return 0;
}
void conversion(int num, int base)
{
      int remainder = num % base;
      if(num == 0)
      {
            return;
      }
      conversion(num / base, base);
      if(remainder < 10)
      {
            printf("%d", remainder);
      }
      else
      {
            printf("%c", remainder - 10 + 'A' );
      }
}

Output:

Enter Decimal Number:   45

Binary Value     :      101101
Octal Value      :      55
Hexadecimal Value:      2D

Post a Comment

3 Comments

  1. how to write c program to multiple two matrix of 4*4

    ReplyDelete
    Replies
    1. https://github.com/Narendarmk/C-Programs/blob/master/matrixMultliplication

      Delete
  2. the declaration of conversion is an implicit function, causing the code to not compile correctly

    ReplyDelete