NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

WAP C or C++ to convert number from random radix system 'r1' to any other random radix system 'r2' where r1! = r2! = 10.

#include<iostream>
using namespace std;
void convertFromDecimal(long long int num, int r)
{
if(num == 0)
return;
convertFromDecimal(num/r, r);
cout<<num%r;
}
int main()
{
int r1,r2;
cout<<"Enter base radix system: ";
cin>>r1;
cout<<"Enter final radix system: ";
cin>>r2;
long long int baseNumber,temp1 = 1, temp2 = 0;
cout<<"Enter base number: ";
cin>>baseNumber;
while(baseNumber != 0){
temp2 += ((baseNumber%10)*temp1);
temp1 *= r1;
baseNumber /= 10;
}

convertFromDecimal(temp2, r2);
return 0;
}



WAP C or C++ to convert number from radix system 2 to radix system 4, 8 and 16 by using relative direct base conversion approach.

#include<iostream>
#include<string>
using namespace std;

int nearest(int l,int m)
{
if(l%m == 0)
return l;
for(int i=l+1; ;i++)
{
if(i>l && i%m==0)
return i;
}
}

int convertToDecimal(string number)
{
int n = 1,finalNum = 0;
int len = number.length();
for(int i=len-1;i>=0;i--)
{
finalNum += ((number[i] - '0')*n);
n *= 2;
}
return finalNum;
}

void convertNumber(string number, int x)
{
int length = number.length();
int newLength = nearest(length,x);
char finalNumber[newLength];
for(int i=0;i<newLength;i++)
finalNumber[i] = '0';
finalNumber[newLength] = '';
for(int i=length-1,j = newLength-1;i>=0 && j>=0;i--,j--)
finalNumber[j] = number[i];
string final = finalNumber;
for(int i=0;i<=newLength-x;i+=x)
cout<<convertToDecimal(final.substr(i,i+x));
cout<<"nn";
return;
}

int main()
{
string binaryNumber;
cout<<"Enter bunary number: ";
getline(cin,binaryNumber);
cout<<"Conversion to base 4: ";
convertNumber(binaryNumber,2);
cout<<"Conversion to base 8: ";
convertNumber(binaryNumber,3);
cout<<"Conversion to base 16: ";
convertNumber(binaryNumber,4);
return 0;
}



WAP C or C++ to convert number from radix system 8 to radix system 2 by using relative direct base conversion approach.

#include<iostream>
#include<string>
using namespace std;

int main()
{
string s[8] = {"000","001","010","011","100","101","110","111"};
cout<<"Enter the number in radix 8: ";
string number;
getline(cin,number);
int n = number.length();
for(int i=0;i<n;i++)
{
cout<<s[number[i] - '0'];
}
return 0;
}



WAP C or C++ to convert number from radix system 16 to radix system 2 by using relative direct base conversion approach.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string s[16] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
cout<<"Enter the number in radix 16: ";
string number;
getline(cin,number);
int n = number.length();
int x;
for(int i=0;i<n;i++)
{
if(number[i] == 'A' || number[i] == 'B' || number[i] == 'C' || number[i] == 'D' || number[i] == 'E' || number[i] == 'F')
x = number[i] - 'A' + 10;
else
x = number[i] - '0';
cout<<s[x];
}
return 0;
}



WAP C or C++ to convert number from radix system 8 to radix system 16 by using relative indirect base conversion approach.

#include<iostream>
#include<string>
using namespace std;

int nearest(int l,int m)
{
if(l%m == 0)
return l;
for(int i=l+1; ;i++)
{
if(i>l && i%m==0)
return i;
}
}

int convertToDecimal(string number)
{
int n = 1,finalNum = 0;
int len = number.length();
for(int i=len-1;i>=0;i--)
{
finalNum += ((number[i] - '0')*n);
n *= 2;
}
return finalNum;
}

string convertToBinary()
{
string s[8] = {"000","001","010","011","100","101","110","111"};
cout<<"Enter the number in radix 8: ";
string number;
getline(cin,number);
int n = number.length();
string returnValue = "";
for(int i=0;i<n;i++)
{
returnValue.append(s[number[i] - '0']);
}
return returnValue;
}

string convertNumber(string number, int x)
{
int length = number.length();
int newLength = nearest(length,x);
char finalNumber[newLength];
for(int i=0;i<newLength;i++)
finalNumber[i] = '0';
finalNumber[newLength] = '';
for(int i=length-1,j = newLength-1;i>=0 && j>=0;i--,j--)
finalNumber[j] = number[i];
string final = finalNumber;
string returnValue = "";
for(int i=0;i<=newLength-x;i+=x)
returnValue.append(to_string(convertToDecimal(final.substr(i,i+x))));
return returnValue;
}

int main()
{
string binaryNumber = convertToBinary();
cout<<"Binary Number is: "<<binaryNumber<<endl;
string finalConverted = convertNumber(binaryNumber,4);
cout<<finalConverted;
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.