Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Thursday, September 29, 2016

Binary To Decimal Conversion

Program To Convert Binary Number To Decimal:


#include<iostream>
#include<math.h> //for power function
using namespace std;
main()
{
    int binary,decimal=0,i=0,remainder;
    cout<<"\nEnter The Binary Number ";
    cin>>binary;
    while(binary!=0)
    {
        remainder=binary%10; // getting the last digit
        binary=binary/10;
        decimal +=remainder*pow(2,i);//finding 2^i*digit
        i++;
    }
    cout<<"Decimal number is "<<decimal;
}

Also See Program To Perform Operations On Queue

Output:

Wednesday, September 28, 2016

Decimal To Hexadecimal Conversion

Program To Convert Decimal Number To Hexadecimal Number:


#include<iostream>
using namespace std;
main()
{
int num,hexa[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
hexa[i]=num%16;
num=num/16;
i++;
}
cout<<"\nHexadecimal Equivalent is ";
for(int j=i-1;j>=0;j--)
{
switch(hexa[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<<hexa[j];
}}
}
Also See Program To Convert Decimal TO Octal Click Here

Output:


Decimal To Binary Conersion

Program to Convert Decimal Number To Binary Equivalent:


#include<iostream>
using namespace std;
main()
{
int num,binary[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
binary[i]=num%2;   //finding remainder
num=num/2;
i++;
}
cout<<"\nBinary Equivalent is ";
for(int j=i-1;j>=0;j--)  //from down to top moving
cout<<binary[j];
}
Also see program to evaluate postfix expression Click Here

Output:


Wednesday, September 21, 2016

Basic To Class Type Conversion

Program To Convert Basic data Type To Class Type:
#include<iostream>
using namespace std;
class time
{
    int hours;
    int minutes;
    public:time()
    {
   
    }
        time(int m)
        { hours=m/60;
        minutes=m%60;
        }
    void display()
    {
        cout<<"\nTime Is "<<hours<<" Hours and "<<minutes<<"Minutes";
        }   
};
main()
{
    time t(165);
    t.display();
   
}