-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBankers Rule.cpp
61 lines (58 loc) · 1.11 KB
/
Bankers Rule.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
61
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
cout<<"Enter Your Number: ";
cin>>str; //1.1255465
int n;
cout<<"Precision: ";
cin>>n;
//getting the position of the decimal point
int point=0;
for(int i=0;i<str.length();i++){
if(str[i]=='.'){
point=i;
break;
}
}
int flag=0;
if(str[point+n+1]>'5'){
int i=point+n;
while(str[i]=='9'){ //while loop is only
str[i]='0'; //for those case like 9.99999 or 3.99999
if(i==0)
flag=1; //for case : 9.999999
i--;
if(str[i]=='.')
i--;
}
if(!flag)
str[i]++;
else
cout<<"1";
cout<<str.substr(0,point+n+1)<<endl;
} else if(str[point+n+1]=='5'){
int temp=str[point+n]-'0';
if(temp%2!=0){
int i=point+n;
while(str[i]=='9'){
str[i]='0';
if(i==0)
flag=1; //for case : 9.999999
i--;
if(str[i]=='.')
i--;
}
if(!flag)
str[i]++;
else
cout<<"1";
cout<<str.substr(0,point+n+1)<<endl;
} else{
cout<<str.substr(0,point+n+1)<<endl;
}
} else{
cout<<str.substr(0,point+n+1)<<endl;
}
return 0;
}