Conversion of Numbers Program in C++


Prev Tutorial Next Tutorial

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.

C++ program to convert decimal to binary

#include<iostream.h>
#include<conio.h>

void main()
{
int no,rem[20],i=0,j;
clrscr();
cout<<"Enter any num : ";
cin>>no;
while(no>0)
{
rem[i]=no%2;
i++;
no=no/2;
}
cout<<"Binary Number is :";
for(j=i-1;j>=0;j--)
{
cout<<rem[j];
}
getch();
}

Output

Enter any number : 7
Binary number is : 111

C++ program to Convert Decimal to Octal

Decimal number have base 10 and Octalnumber have base 8.

C++ program to convert Decimal to Octal

#include<iostream.h>
#include<conio.h>

void main()
{
int no,rem[20],i=0,j;
clrscr();
cout<<"Enter any num : ";
cin>>no;
while(no>0)
{
rem[i]=no%8;
i++;
no=no/8;
}
cout<<"Binary Number is :";
for(j = i - 1; j > = 0 ; j--)
{
cout<<rem[j];
}
getch();
}

Output

Enter any number : 23
Binary number is : 27

C++ program to Convert Decimal to Hexadecimal

Decimal number have base 10 and Hexadecimal number have base 16.

c++ program to convert Decimal to Hexadecimal

#include<iostream.h>
#include<conio.h>

void main()
{
int num,rem[20],hex=0,i=0,j;
clrscr();
cout<<"Enter any Number: ";
cin>>num;
while(num>0)
  {
    rem[i] = num % 16;
    num = num / 16;
    i++;
  }
cout<<"Hexadecimal number : ";
for(j = i - 1 ; j > = 0 ; j--)
  {
    switch(rem[j])
    {
      case 10:
	  cout<<"A";
	  break;
      case 11:
	  cout<<"B";
	  break;
      case 12:
	  cout<<"C";
	  break;
      case 13:
	  cout<<"D";
	  break;
      case 14:
	  cout<<"E";
	  break;
      case 15:
	  cout<<"F";
	  break;
      default :
	 cout<<rem[j];
    }
  }
getch();
}

Output

Enter any number : 231
Hexadecimal number : E7

Prev Tutorial Next Tutorial

Advertisements
Download Projects

Advertisements

Download WhatsApp Status Video

Buy This Ad Space Only $50 per Month, Ad Size 300X250 Contact on: [email protected] WhatsApp no. 8882538826

Google Adsense Advertisements