-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP47FUNCTION.cpp
More file actions
51 lines (41 loc) · 762 Bytes
/
P47FUNCTION.cpp
File metadata and controls
51 lines (41 loc) · 762 Bytes
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
// Decimal to Binary
#include <iostream>
using namespace std;
// int DecimalToBinary(int n)
// {
// int ans=0;
// int x=1;
// while (x <= n)
// {
// x *= 2;
// }
// x/=2;
// while(x>0)
// {
// int lastDigit = n/x; // to bring quotient
// n -= lastDigit*x;
// x/=2;
// ans = ans*10 + lastDigit;
// }
// return ans;
// }
int DecimalToBinary(int n)
{
int ans=0;
int x=1;
while(n>0)
{
int y = n%2;
ans += x*y;
n/=2;
x*=10;
}
return ans;
}
int main(){
int n;
cout<<"enter a number:";
cin>>n;
cout<<DecimalToBinary(n)<<endl;
return 0;
}