Program to Convert Decimal to Hexadecimal
- Write a program to Convert Decimal to Hexadecimal in C
- Write a program to Convert Decimal to Hexadecimal in C++
- Write a program to Convert Decimal to Hexadecimal in Python
- Write a program to Convert Decimal to Hexadecimal in PHP
- Write a program to Convert Decimal to Hexadecimal in Java
- Write a program to Convert Decimal to Hexadecimal in Java Script
- Write a program to Convert Decimal to Hexadecimal in C#
Explanation:
A decimal number (base 10) is converted to its hexadecimal equivalent (base 16) by continuously dividing it by 16 and noting the remainders. The hexadecimal digits in reverse order are represented by these remainders. The letters A through F stand for digits larger than 9 in hexadecimal, where:
A=10
B = 11
C = 12
D = 13
E = 14
F = 15
Steps for Decimal to Hexadecimal Conversion:
- Start with the decimal number.
- For example: n=254
- Divide the number by 16.
- Record the remainder. If the remainder is 10 to 15, convert it to A to F.
- Update the number as the quotient of the division.
- Repeat the division process until the quotient becomes 0
- The hexadecimal number is the sequence of remainders read in reverse order.
Example Conversion:
Convert 254 to hexadecimal:
- 254 ÷ 16 = 15 remainder 14 → Hexadecimal digit: E
- 15 ÷ 16 = 0 remainder 15 → Hexadecimal digit: F
Hexadecimal equivalent (reversed remainders): FE.
Program to Convert Decimal to Hexadecimal
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> void decimalToHexadecimal(int n) { char hex[100]; int i = 0; while (n != 0) { int temp = n % 16; if (temp < 10) hex[i] = temp + 48; else hex[i] = temp + 55; n /= 16; i++; } for (int j = i - 1; j >= 0; j--) printf("%c", hex[j]); } int main() { int n; printf("Enter a decimal number: "); scanf("%d", &n); printf("Hexadecimal: "); decimalToHexadecimal(n); return 0; }
#include <iostream> using namespace std; void decimalToHexadecimal(int n) { string hex = ""; char hexChars[] = "0123456789ABCDEF"; while (n > 0) { hex = hexChars[n % 16] + hex; n /= 16; } cout << hex; } int main() { int n; cout << "Enter a decimal number: "; cin >> n; cout << "Hexadecimal: "; decimalToHexadecimal(n); return 0; }
def decimal_to_hexadecimal(n): return hex(n).replace("0x", "").upper() n = int(input("Enter a decimal number: ")) print("Hexadecimal:", decimal_to_hexadecimal(n))
<?php function decimalToHexadecimal($n) { echo strtoupper(dechex($n)); } $n = (int)readline("Enter a decimal number: "); echo "Hexadecimal: "; decimalToHexadecimal($n); ?>
import java.util.Scanner; public class DecimalToHexadecimal { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a decimal number: "); int n = sc.nextInt(); System.out.println("Hexadecimal: " + Integer.toHexString(n).toUpperCase()); } }
function decimalToHexadecimal(n) { return n.toString(16).toUpperCase(); } const n = parseInt(prompt("Enter a decimal number: ")); console.log("Hexadecimal:", decimalToHexadecimal(n));
using System; class DecimalToHexadecimal { static void Main() { Console.Write("Enter a decimal number: "); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Hexadecimal: " + n.ToString("X")); } }