Q:

Write a program in C++ to convert a decimal number to hexadecimal number

0

Write a program in C++ to convert a decimal number to hexadecimal number

Sample Output:

 Convert a decimal number to hexadecimal number:                       
---------------------------------------------------                    
 Input a decimal number: 43                                            
 The hexadecimal number is : 2B

All Answers

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

#include <iostream>
#include <math.h>
using namespace std;
 
int main()
{
    int dec_num, r;
    string hexdec_num="";
    char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	cout << "\n\n Convert a decimal number to hexadecimal number:\n";
	cout << "---------------------------------------------------\n";
	cout << " Input a decimal number: ";
	cin>> dec_num;
		
        while(dec_num>0)
        {
            r = dec_num % 16;
            hexdec_num = hex[r] + hexdec_num;
            dec_num = dec_num/16;
        }
        cout<<" The hexadecimal number is : "<<hexdec_num<<"\n"; 
	}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now