C++ Program – Convert decimal to binary & binary to decimal

by Nideesh C on May 2, 2011 · 0 comments

in C++




//Downloaded From theonlinetutorials.com
//(C)2011.All rights reserved.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a,g,h,s=0,i=0,d,b,ch;
cout<<endl<<"Enter the choice - "<<endl<<"1:Binary to Decimal"<<endl<<"2:Decimal to Binary"<<endl;
cin>>ch;
switch(ch)
{
case 1:

cout<<"Enter the Binary Digit"<<endl;
cin>>b;
while(b>0)
{
a=b%10;
s=s+a*(pow(2,i));
i=i+1;
b=b/10;
}
cout<<"Answer ="<<s<<endl;
break;

case 2:
cout<<"Enter the Decimal"<<endl;
cin>>d;
while(d>0)
{
a=d%2;
a=a*pow(10,i);
s=s+a;
i=i+1;
d=d/2;
}
cout<<"Answer ="<<s<<endl;
break;

default:
cout<<"Wrong choice"<<endl;
break;
}
getch();

}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to convert the given binary number into decimal
  2. C program to convert the given binary number into its equivalent decimal
  3. C program to convert the given binary number into its equivalent decimal
  4. C program to accept a decimal number and conert it binary and count the number of 1′s in the binary number
  5. C++ Program – Calculator

Leave a Comment

Previous post:

Next post: