Number System Conversion Program In C
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.
- Number System Conversion Pdf
- Number System In C
- Number System Conversion Tutorial
- Number System Conversion Program
- 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 −
Step | Operation | Result | Remainder |
---|---|---|---|
Step 1 | 29 / 2 | 14 | 1 |
Step 2 | 14 / 2 | 7 | 0 |
Step 3 | 7 / 2 | 3 | 1 |
Step 4 | 3 / 2 | 1 | 1 |
Step 5 | 1 / 2 | 0 | 1 |
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 −
Step | Binary Number | Decimal Number |
---|---|---|
Step 1 | 111012 | ((1 × 24) + (1 × 23) + (1 × 22) + (0 × 21) + (1 × 20))10 |
Step 2 | 111012 | (16 + 8 + 4 + 0 + 1)10 |
Step 3 | 111012 | 2910 |
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
Step | Octal Number | Decimal Number |
---|---|---|
Step 1 | 258 | ((2 × 81) + (5 × 80))10 |
Step 2 | 258 | (16 + 5 )10 |
Step 3 | 258 | 2110 |
Octal Number − 258 = Decimal Number − 2110
Step 2 − Convert Decimal to Binary
Step | Operation | Result | Remainder |
---|---|---|---|
Step 1 | 21 / 2 | 10 | 1 |
Step 2 | 10 / 2 | 5 | 0 |
Step 3 | 5 / 2 | 2 | 1 |
Step 4 | 2 / 2 | 1 | 0 |
Step 5 | 1 / 2 | 0 | 1 |
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 −
Step | Binary Number | Octal Number |
---|---|---|
Step 1 | 101012 | 010 101 |
Step 2 | 101012 | 28 58 |
Step 3 | 101012 | 258 |
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 −
Step | Octal Number | Binary Number |
---|---|---|
Step 1 | 258 | 210 510 |
Step 2 | 258 | 0102 1012 |
Step 3 | 258 | 0101012 |
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 −
Step | Binary Number | Hexadecimal Number |
---|---|---|
Step 1 | 101012 | 0001 0101 |
Step 2 | 101012 | 110 510 |
Step 3 | 101012 | 1516 |
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 −
Step | Hexadecimal Number | Binary Number |
---|---|---|
Step 1 | 1516 | 110 510 |
Step 2 | 1516 | 00012 01012 |
Step 3 | 1516 | 000101012 |
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:
- 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.
- Divide the number by 16 now
- Repeat the above two steps until the number is not equal to 0.
- 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> void decToHexa( int n) // char array to store hexadecimal number int i = 0; { int temp = 0; // storing remainder in temp variable. if (temp < 10) hexaDeciNum[i] = temp + 48; } { i++; } // printing hexadecimal number array in reverse order cout << hexaDeciNum[j]; int main() int n = 2545; decToHexa(n); return 0; |
Java
// number to hexadecimal number { static void decToHexa( int n) // char array to store hexadecimal number int i = 0 ; { int temp = 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]); public static void main (String[] args) int n = 2545 ; } |
Python3
# convert a decimal # number # function to convert def decToHexa(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 int i = 0; { // store remainder // variable. if (temp < 10) hexaDeciNum[i] = ( char )(temp + 48); } { i++; } // printing hexadecimal number for ( int j = 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 function decToHexa( $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.