From 6d91e800be71e8b154eae12622492f8bd04fdf9c Mon Sep 17 00:00:00 2001 From: Nokhalal Mahato Date: Tue, 29 Mar 2022 18:15:50 +0530 Subject: [PATCH] chore(CPlusPlus): add number system (#714) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- .../Number system/binary_addition.cpp | 85 +++++++++++++++++++ .../Number system/binary_to_decimal.cpp | 18 ++++ .../Number system/decimal_to_binary.cpp | 24 ++++++ .../Number system/decimal_to_hexa.cpp | 29 +++++++ .../Number system/decimal_to_octal.cpp | 24 ++++++ .../Number system/hexa_to_decimal.cpp | 24 ++++++ .../Number system/octal_to_decimal.cpp | 20 +++++ algorithms/CPlusPlus/README.md | 11 +++ 8 files changed, 235 insertions(+) create mode 100644 algorithms/CPlusPlus/Number system/binary_addition.cpp create mode 100644 algorithms/CPlusPlus/Number system/binary_to_decimal.cpp create mode 100644 algorithms/CPlusPlus/Number system/decimal_to_binary.cpp create mode 100644 algorithms/CPlusPlus/Number system/decimal_to_hexa.cpp create mode 100644 algorithms/CPlusPlus/Number system/decimal_to_octal.cpp create mode 100644 algorithms/CPlusPlus/Number system/hexa_to_decimal.cpp create mode 100644 algorithms/CPlusPlus/Number system/octal_to_decimal.cpp diff --git a/algorithms/CPlusPlus/Number system/binary_addition.cpp b/algorithms/CPlusPlus/Number system/binary_addition.cpp new file mode 100644 index 00000000..c75918af --- /dev/null +++ b/algorithms/CPlusPlus/Number system/binary_addition.cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +int reverse(int n){ + int ans=0,d; + while(n>0){ + d=n%10; + ans=ans*10 + d; + n=n/10; + } + return ans; +} + +int binary_addition(int a,int b){ + int add=0,carry=0; + while(a>0 && b>0){ + + if(a%2==1 && b%2==1){ + add=add*10 + carry; + carry=1; + } + else if((a%2==1 && b%2==0) || (a%2==0 && b%2==1)){ + if(carry==1){ + add=add*10 + 0; + carry=1; + } + else{ + add=add*10 + 1; + carry=0; + } + } + else{ + add = add*10 + carry; + carry=0; + } + a=a/10; + b=b/10; + } + while(a>0){ + if(a%2==1){ + if(carry==1){ + carry=1; + add=add*10+0; + } + else{ + carry=0; + add=add*10 + 1; + } + } + else{ + add=add*10 +carry; + carry=0; + } + a=a/10; + } + while(b>0){ + if(b%2==1){ + if(carry==1){ + carry=1; + add=add*10+0; + } + else{ + carry=0; + add=add*10 + 1; + } + } + else{ + add=add*10 +carry; + carry=0; + } + b=b/10; + } + if(carry==1){ + add=add*10 +1; + } + add=reverse(add); + return add; +} + +int main(){ + int a,b; + cin>>a>>b; + int add=binary_addition(a,b); + cout< +using namespace std; +int binarytodecimal(int n){ + int two=1,r,num=0; + while(n>0){ + r=n%10; + num=num + two*r; + two*=2; + n/=10; + } + return num; +} +int main(){ + int n; + cin>>n; + int decimal=binarytodecimal(n); + cout< +using namespace std; + +int decimaltobinary(int n){ + int max=1; + while(max<=n){ + max=max*2; + } + max/=2; + int num=0; + while(max>=1){ + num=num*10 + n/max; + n-=max*(n/max); + max=max/2; + } + return num; +} + +int main(){ + int n; + cin>>n; + int binary=decimaltobinary(n); + cout< +using namespace std; + +string decimal_to_hexa(int n){ + int multiplier=1; + while(multiplier<=n) + multiplier*=16; + multiplier/=16; + string num=""; + int d; + while(multiplier>=1){ + d=n/multiplier; + if(d>9){ + char c='A'+(d-10); + num= num + c; + } + else + num=num + to_string(d); + n=n-(d*multiplier); + multiplier/=16; + } + return num; +} +int main(){ + int n; + cin>>n; + string hexa=decimal_to_hexa(n); + cout< +using namespace std; + +int decimal_to_octal(int n){ + int multiplier=1; + while(multiplier<=n){ + multiplier*=8; + } + + multiplier/=8; + int num=0; + while(multiplier>=1){ + num=num*10 + n/multiplier; + n=n - multiplier*(n/multiplier); + multiplier/=8; + } + return num; +} +int main(){ + int n; + cin>>n; + int octal=decimal_to_octal(n); + cout< +using namespace std; + +int hexa_to_decimal(string n){ + int l=n.size(); + l=l-1; + int ans=0,x=1; + while(l>=0){ + if(n[l]>='0' && n[l]<='9') + ans+=(n[l]-'0')*x; + else + ans+=x*(n[l]-'A'+10); + x*=16; + l--; + } + return ans; +} + +int main(){ + string s; + cin>>s; + int decimal=hexa_to_decimal(s); + cout< +using namespace std; + +int octal_to_decimal(int n){ + int num=0,r,multiplier=1; + while(n>0){ + r=n%10; + num=num + (r*multiplier); + n=n/10; + multiplier=multiplier*8; + } + return num; +} + +int main(){ + int n; + cin>>n; + int decimal=octal_to_decimal(n); + cout<