chore(CPlusPlus): add number system (#714)

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/696/head^2
Nokhalal Mahato 2022-03-29 18:15:50 +05:30 committed by GitHub
parent b7fa62ce5d
commit 6d91e800be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 235 additions and 0 deletions

View File

@ -0,0 +1,85 @@
#include<bits/stdc++.h>
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<<add;
}

View File

@ -0,0 +1,18 @@
#include<bits/stdc++.h>
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<<decimal;
}

View File

@ -0,0 +1,24 @@
#include<bits/stdc++.h>
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<<binary;
}

View File

@ -0,0 +1,29 @@
#include<bits/stdc++.h>
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<<hexa;
}

View File

@ -0,0 +1,24 @@
#include<bits/stdc++.h>
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<<octal;
}

View File

@ -0,0 +1,24 @@
#include<bits/stdc++.h>
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<<decimal;
}

View File

@ -0,0 +1,20 @@
#include<bits/stdc++.h>
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<<decimal;
}

View File

@ -185,3 +185,14 @@
- [Product of two numbers](Recursion\product-of-numbers.cpp) - [Product of two numbers](Recursion\product-of-numbers.cpp)
- [Product of digits in a number](Recursion\product-of-digits.cpp) - [Product of digits in a number](Recursion\product-of-digits.cpp)
- [Linear search using recursion](Recursion/linear-search.cpp) - [Linear search using recursion](Recursion/linear-search.cpp)
## Number System
- [Binary Addition](Number-system/binary_addition.cpp)
- [Binary to Decimal](Number-system/binary_to_decimal.cpp)
- [Decimal To Binary](Number-system/decimal_to_binary.cpp)
- [Decimal To Hexa-Decimal](Number-system/decimal_to_hexa.cpp)
- [Hexa-Decimal To Decimal](Number-system/hexa_to_decimal.cpp)
- [Decimal To Octal](Number-system/decimal_to_octal.cpp)
- [Octal To Decimal](Number-system/octal_to_decimal.cpp)