Number System Conversion Program In C

/ Comments off


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.

  1. Number System Conversion Pdf
  2. Number System In C
  3. Number System Conversion Program In Canada
  4. Number System Conversion Program In C
  5. Sample Program In C++ Programming

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. Wap in C to convert a base 2 number to a base 10 number. Required Knowledge. This program converts a binary number( base 2) to decimal number (base 10). Binary number system is a base 2 number system using digits 0 and 1 whereas Decimal number system is base 10 and using digits from 0 to 9. Hexadecimal to Octal Conversion in C++. To convert hexadecimal number to octal number in C++ programming, you have to ask to the user to enter the hexadecimal number to convert it into octal number to display the equivalent value in octal format as shown here in the following program.

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:

Number system conversion program in c

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.

Gateway model mfatxnin nmz 300s drivers. 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;

Number System In C

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

Number System Conversion Program In Canada

// 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:

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.

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


Number System Conversion Program In C

Recommended Posts:

Sample Program In C++ Programming