RUN


Convert Decimal-Hexadecimal program

A decimal number is taken as input and converted it into its hexadecimal equivalent.

Example:

Decimal number: 77

Corresponding hexadecimal number: 4D

Decimal number: 50

Corresponding hexadecimal number: 32

Algorithm:

  1. A decimal number is taken as input.
  2. The number is divided by 16 and the remainders are stored.
  3.  The result is printed in reverse order.

Code:

#include<iostream>
using namespace std;

int main()

{

        long int dno, temp;

        char hex[100];

        int j, remainder, i=1;

        cout<<"Enter Decimal Number : ";

        cin>>dno;

        temp = dno;

        while(temp!=0)

        {

                remainder = temp%16;

                if(remainder<10)   // Converts integer into character

                {

                        remainder = remainder + 48;

                }

                else

                {

                        remainder = remainder + 55;

                }

                hex[i++] = remainder;

                temp = temp/16;

        }

        cout<<"\nHexadecimal Number corresponding to Decimal Number : ";

        for(j=i-1; j>0; j--)

        {

                cout<<hex[j];

        }

        return 0;

}

Report Error/ Suggestion

Related Posts:

[yuzo_views]



Interview Questions



CopyRight © 2020

CopyRight © 2020