Number System Conversion Program In C

03.02.2020by

Number Conversion Program in C++. With the help of C++ Language you can convert any number from decimal to binary and binary to decimal, binary to octal, binary to hexadecimal, decimal to octal and etc. Decimal to Binary Conversion. Decimal number have base 10 and Binary number have base 2. Write a C program to read octal number from user and convert to hexadecimal number system. How to convert from octal number system to hexadecimal number system in C programming. Logic to convert octal to hexadecimal number system in C program.

  1. Number System Conversion Pdf
  2. Number System In C
  3. Number System Conversion Tutorial
  4. Number System Conversion Program
  5. Sample Program In C++ Programming
  • Computer Organization Tutorial
  • Computer Organization Resources
  • Selected Reading

There are many methods or techniques which can be used to convert numbers from one base to another. We'll demonstrate here the following −

  • Decimal to Other Base System
  • Other Base System to Decimal
  • Other Base System to Non-Decimal
  • Shortcut method − Binary to Octal
  • Shortcut method − Octal to Binary
  • Shortcut method − Binary to Hexadecimal
  • Shortcut method − Hexadecimal to Binary

Decimal to Other Base System

Steps

  • Step 1 − Divide the decimal number to be converted by the value of the new base.

  • Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number.

  • Step 3 − Divide the quotient of the previous divide by the new base.

  • Step 4 − Record the remainder from Step 3 as the next digit (to the left) of the new base number.

Repeat Steps 3 and 4, getting remainders from right to left, until the quotient becomes zero in Step 3.

The last remainder thus obtained will be the Most Significant Digit (MSD) of the new base number.

Example −

Decimal Number: 2910

Calculating Binary Equivalent −

StepOperationResultRemainder
Step 129 / 2141
Step 214 / 270
Step 37 / 231
Step 43 / 211
Step 51 / 201

As mentioned in Steps 2 and 4, the remainders have to be arranged in the reverse order so that the first remainder becomes the Least Significant Digit (LSD) and the last remainder becomes the Most Significant Digit (MSD).

Decimal Number − 2910 = Binary Number − 111012.

Other Base System to Decimal System

Steps

  • Step 1 − Determine the column (positional) value of each digit (this depends on the position of the digit and the base of the number system).

  • Step 2 − Multiply the obtained column values (in Step 1) by the digits in the corresponding columns.

  • Step 3 − Sum the products calculated in Step 2. The total is the equivalent value in decimal.

Example

Binary Number − 111012

Calculating Decimal Equivalent −

StepBinary NumberDecimal Number
Step 1111012((1 × 24) + (1 × 23) + (1 × 22) + (0 × 21) + (1 × 20))10
Step 2111012(16 + 8 + 4 + 0 + 1)10
Step 31110122910

Binary Number − 111012 = Decimal Number − 2910

Other Base System to Non-Decimal System

Steps

  • Step 1 − Convert the original number to a decimal number (base 10).

  • Step 2 − Convert the decimal number so obtained to the new base number.

Example

Octal Number − 258

Calculating Binary Equivalent −

Step 1 − Convert to Decimal

StepOctal NumberDecimal Number
Step 1258((2 × 81) + (5 × 80))10
Step 2258(16 + 5 )10
Step 32582110

Octal Number − 258 = Decimal Number − 2110

Step 2 − Convert Decimal to Binary

StepOperationResultRemainder
Step 121 / 2101
Step 210 / 250
Step 35 / 221
Step 42 / 210
Step 51 / 201

Decimal Number − 2110 = Binary Number − 101012

Octal Number − 258 = Binary Number − 101012

Shortcut method - Binary to Octal

Steps

  • Step 1 − Divide the binary digits into groups of three (starting from the right).

  • Step 2 − Convert each group of three binary digits to one octal digit.

Example

Binary Number − 101012

Calculating Octal Equivalent −

StepBinary NumberOctal Number
Step 1101012010 101
Step 210101228 58
Step 3101012258

Binary Number − 101012 = Octal Number − 258

Shortcut method - Octal to Binary

Steps

  • Step 1 − Convert each octal digit to a 3 digit binary number (the octal digits may be treated as decimal for this conversion).

  • Step 2 − Combine all the resulting binary groups (of 3 digits each) into a single binary number.

Example

Octal Number − 258

Calculating Binary Equivalent −

StepOctal NumberBinary Number
Step 1258210 510
Step 22580102 1012
Step 32580101012

Octal Number − 258 = Binary Number − 101012

Shortcut method - Binary to Hexadecimal

Steps

  • Step 1 − Divide the binary digits into groups of four (starting from the right).

  • Step 2 − Convert each group of four binary digits to one hexadecimal symbol.

Example

Binary Number − 101012

Calculating hexadecimal Equivalent −

StepBinary NumberHexadecimal Number
Step 11010120001 0101
Step 2101012110 510
Step 31010121516

Binary Number − 101012 = Hexadecimal Number − 1516

Shortcut method - Hexadecimal to Binary

Steps

  • Step 1 − Convert each hexadecimal digit to a 4 digit binary number (the hexadecimal digits may be treated as decimal for this conversion).

  • Step 2 − Combine all the resulting binary groups (of 4 digits each) into a single binary number.

Example

Hexadecimal Number − 1516

Calculating Binary Equivalent −

StepHexadecimal NumberBinary Number
Step 11516110 510
Step 2151600012 01012
Step 31516000101012

Hexadecimal Number − 1516 = Binary Number − 101012


Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent hexadecimal number. i.e convert the number with base value 10 to base value 16. Tsstcorp cddvdw ts-l633c ata device drivers windows 7.

Hexadecimal numbers uses 16 values to represent a number. Numbers from 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F.

Examples:

Download ebook motivasi dan pengembangan diri gratis 2016 popular. Algorithm:

  1. Store the remainder when the number is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array otherwise if temp is greater than or equals to 10, insert (55 + temp) in the character array.
  2. Divide the number by 16 now
  3. Repeat the above two steps until the number is not equal to 0.
  4. Print the array in reverse order now.

Example

If the given decimal number is 2545.
Step 1: Calculate remainder when 2545 is divided by 16 is 1. Therefore, temp = 1. As temp is less than 10. So, arr[0] = 48 + 1 = 49 = ‘1’.
Step 2: Divide 2545 by 16. New number is 2545/16 = 159.
Step 3: Calculate remainder when 159 is divided by 16 is 15. Therefore, temp = 15. As temp is greater than 10. So, arr[1] = 55 + 15 = 70 = ‘F’.
Step 4: Divide 159 by 16. New number is 159/16 = 9.
Step 5: Calculate remainder when 9 is divided by 16 is 9. Therefore, temp = 9. As temp is less than 10. So, arr[2] = 48 + 9 = 57 = ‘9’.
Step 6: Divide 9 by 16. New number is 9/16 = 0.
Step 7: Since number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore the equivalent hexadecimal number is 9F1.

Below diagram shows an example of converting the decimal number 2545 to equivalent hexadecimal number.

Below is the implementation of above idea.

Number System Conversion Pdf

C++

// number to hexadecimal number
#include<iostream>
voiddecToHexa(intn)
// char array to store hexadecimal number
inti = 0;
{
inttemp = 0;
// storing remainder in temp variable.
if(temp < 10)
hexaDeciNum[i] = temp + 48;
}
{
i++;
}
// printing hexadecimal number array in reverse order
cout << hexaDeciNum[j];
intmain()
intn = 2545;
decToHexa(n);
return0;

Java

// number to hexadecimal number
{
staticvoiddecToHexa(intn)
// char array to store hexadecimal number
inti = 0;
{
inttemp = 0;
// storing remainder in temp variable.
if(temp < 10)
hexaDeciNum[i] = (char)(temp + 48);
}
{
i++;
}
// printing hexadecimal number array in reverse order
System.out.print(hexaDeciNum[j]);
publicstaticvoidmain (String[] args)
intn = 2545;
}

Python3

# convert a decimal
# number
# function to convert
defdecToHexa(n):
# char array to store
hexaDeciNum =['0'] *100;
# counter for hexadecimal
i =0;
# to store remainder
# in temp variable.
if(temp < 10):
i =i +1;
hexaDeciNum[i] =chr(temp +55);
n =int(n /16);
# printing hexadecimal number
j =i -1;
print((hexaDeciNum[j]), end =');
n =2545;
# by mits.

C#

// number to hexadecimal number
{
// to hexadecimal
{
// hexadecimal number
inti = 0;
{
// store remainder
// variable.
if(temp < 10)
hexaDeciNum[i] = (char)(temp + 48);
}
{
i++;
}
// printing hexadecimal number
for(intj = i - 1; j >= 0; j--)
}
// Driver Code
{
decToHexa(n);
}
// This code is contributed by Nitin Mittal.

PHP

// PHP program to convert
// hexadecimal number
// function to convert
functiondecToHexa($n)
// char array to store
$hexaDeciNum;
// counter for hexadecimal
$i= 0;
{
// to store remainder
// in temp variable.
if($temp< 10)
$hexaDeciNum[$i] = chr($temp+ 48);
}
{
$i++;
}
// printing hexadecimal number
for($j= $i- 1; $j>= 0; $j--)
}
// Driver Code
decToHexa($n);
// This code is contributed
?>

Output:

Number System In C

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Number System Conversion Tutorial

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Number System Conversion Program

Recommended Posts:

Sample Program In C++ Programming


Comments are closed.