C and C++

Program to convert decimal to binary using templates

#include<iostream.h>
#include<math.h>
template<class T>
class convert
{
private:
int base, rem, t1;
long y;

public:
void asctobin(T p)
{
y=base=0;
t1=p;
while(t1>0)
{
rem = t1%2;
y = y+rem*pow(10,base);
base++;
t1=t1/2;
}
}
void display()
{
cout<<"Binary value is: "<<y<<endl;
}
};
void main()
{
convert c;
int num;
cout<<"Enter the decimal number: "; cin>>num;
a.asctobin(num);
c.display();
cout<<endl;
}

[wp_ad_camp_1]

 OUTPUT
Enter the decimal number: 124
Binary value is: 1111100

Leave a comment

Your email address will not be published. Required fields are marked *