Repository navigation
Expand file tree
/
Copy pathLecture6.cpp
More file actions
74 lines (50 loc) · 1.53 KB
/
Copy pathLecture6.cpp
File metadata and controls
74 lines (50 loc) · 1.53 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// #include<iostream>
// #include<cmath>
// using namespace std;
// int main(){
// ------------:::::::::::::CONVERT DECIMAL TO BINARY:::::::::::::-----------
// positive number
// int n;
// cin>>n;
// int i = 0, ans = 0;
// while(n!=0){
// int bit = n&1;
// ans = (bit * pow( 10 , i) ) + ans; // (FORMULA) for getting binary value of a decimal.
// n = n >> 1;
// i++;
// }
// cout<<ans;
// formula for exact printing the number without reversing is :---
// ans = (ans * 10) + digit
// formula for reversing the number is :---
// ans = (digit * pow(10,i)) + ans
// ------------:::::::::::::CONVERT BINARY TO DECIMAL:::::::::::::-----------
// 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<<ans;
// }
// ------------:::::::::::::CONVERT NEGATIVE DECIMAL TO BINARY :::::::::::::-----------
// int n;
// cin>>n;
// int i = 0;
// float ans = 0;
// int r = 1 +(~n); //-n bhi likh skte hai
// while(r!=0){
// int bit = r&1; //checks lsb and stores it in ans
// ans = (bit * pow(10,i)) + ans;
// r = r>>1;
// i++;
// }
// cout<<ans;
// return 0;
// }