Q:

C Program to convert decimal number to Binary, Octal or Hexadecimal

0

Write a C Program to convert positive decimal number to Binary, Octal or Hexadecimal. Here’s a Simple Program to convert decimal number to Binary, Octal or Hexadecimal number in C Programming Language.

Create a program that would convert a decimal number to binary, octal or hexadecimal counterpart. Your program should ask the user for decimal with a data type of a long integer.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Decimal system : :


  • Decimal system is the most widely used number system. But computer only understands binary. Binary, octal and hexadecimal number systems are closely related and we may require to convert decimal into these systems.
  • Decimal system is base 10 (ten symbols, 0-9, are used to represent a number) and similarly, binary is base 2, octal is base 8 and hexadecimal is base 16.

Below is the source code for C Program to convert positive decimal number to Binary, Octal or Hexadecimal which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* Program to convert a positive decimal number to Binary, Octal or Hexadecimal */


#include<stdio.h>
void convert(int, int);

int main()
{
        int num;
        printf("Enter a positive decimal number : ");
        scanf("%d", &num);
        printf("\nBinary number :: ");
        convert(num, 2);
        printf("\n");
        printf("\nOctal number :: ");
        convert(num, 8);
        printf("\n");
        printf("\nHexadecimal number :: ");
        convert(num, 16);
        printf("\n");

        return 0;
}/*End of main()*/

void convert (int num, int base)
{
        int rem = num%base;

        if(num==0)
                return;
        convert(num/base, base);

        if(rem < 10)
                printf("%d", rem);
        else
                printf("%c", rem-10+'A' );
}/*End of convert()*/

OUTPUT  : :


//----------------------OUTPUT ::---------------------------------


//----------------------FIRST RUN ::------------------------------

Enter a positive decimal number : 15

Binary number :: 1111

Octal number :: 17

Hexadecimal number :: F


//-----------------------SECOND RUN ::----------------------------

Enter a positive decimal number : 157

Binary number :: 10011101

Octal number :: 235

Hexadecimal number :: 9D

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Recursion Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to display reverse number and fi... >>
<< Write a C Program to display numbers from 1 to n a...