top of page
Search
Writer's pictureParas Saini

Do you ever tried to write a program for conversion of a number from any base to any another base.

Let's discuss about some input parameters and their constraints,


Input:- first line will contain Sb (source number system base), Db (destination number system base) and Sn (number in source format).


Output:- You have to convert Source number to the number with base as db.


Constraints:- 0 < Sn <= 1000000000


First of all have a glance at this beautiful lines of code ;)

#include<iostream> #include<bits/stdc++.h> using namespace std; #define ll long long int int main() {

//Here we are taking Input ll sb,db; cin>>sb>>db;

//We are taking Source Number in string to handle large values in Binary :) string sn; cin>>sn;

//First of all convert the number into decimal,so cvrt here is used to get the final decimal number. ll cvrt=0; ll help=1; for(ll i=sn.length()-1;i>=0;i--) { cvrt+=(sn[i]-'0')*help; help*=sb; }

//Now to convert a number to any base,the formula is to take remainder of number with required base,that's what we are doing here. string str; while(cvrt) { str+=to_string(cvrt%db); cvrt/=db; }

//To get the final answer,you just need to reverse the string. reverse(str.begin(),str.end()); cout<<str<<endl; return 0; }

Hope you got some idea about the former.

Thanks.

92 views0 comments

Recent Posts

See All

Comments


bottom of page