forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_decimal_converter.cpp
60 lines (52 loc) · 1.28 KB
/
binary_decimal_converter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*This a c++ program which can convert a decimal number into a binary number
and a binary number into a decimal number...*/
#include<iostream>
#include<math.h>
using namespace std;
void binary_to_decimal(){
cout<<"Enter binary to convert it into number"<<endl;
int n;
cin>>n;
int ans=0;
int i=0;
while(n!=0){
int digit=n%10;
if(digit==1){
ans=ans+pow(2,i);
}
n=n/10;
i++;
}
cout<<"Answer is "<<ans<<endl;
}
void decimal_to_binary(){
cout<<"Enter number to convert it into binary"<<endl;
int n;
cin>>n;
int ans=0;
int i=0;
while(n!=0){
int bit=n&1;
ans=(bit*pow(10,i))+ans;
n=n>>1;
i++;
}
cout<<"Answer is " <<ans<<endl;
}
int main(){
cout<<"Welcome to binary and decimal converter Program"<<endl;
cout<<"Please Enter Your Choice"<<endl<<"1.Convert Binary into Decimal"<<endl<<"2.Convert Decimal into Binary"<<endl;
int ch;
cin>>ch;
switch(ch){
case 1:
binary_to_decimal();
break;
case 2:
decimal_to_binary();
break;
default:
cout<<"Enter a valid option!"<<endl;
break;
}
}