এই টিউটোরিয়ালে, আমরা একটি সংখ্যাকে তার নেতিবাচক ভিত্তি প্রতিনিধিত্বে রূপান্তর করার জন্য একটি প্রোগ্রাম নিয়ে আলোচনা করব।
এর জন্য আমাদের একটি সংখ্যা এবং সংশ্লিষ্ট নেতিবাচক ভিত্তি প্রদান করা হবে। আমাদের কাজ হল প্রদত্ত সংখ্যাটিকে তার ঋণাত্মক বেস সমতুল্যে রূপান্তর করা। আমরা নেতিবাচক ভিত্তি মানের জন্য শুধুমাত্র -2 এবং -10 এর মধ্যে মান অনুমোদন করছি৷
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
//converting integer into string
string convert_str(int n){
string str;
stringstream ss;
ss << n;
ss >> str;
return str;
}
//converting n to negative base
string convert_nb(int n, int negBase){
//negative base equivalent for zero is zero
if (n == 0)
return "0";
string converted = "";
while (n != 0){
//getting remainder from negative base
int remainder = n % negBase;
n /= negBase;
//changing remainder to its absolute value
if (remainder < 0) {
remainder += (-negBase);
n += 1;
}
// convert remainder to string add into the result
converted = convert_str(remainder) + converted;
}
return converted;
}
int main() {
int n = 9;
int negBase = -3;
cout << convert_nb(n, negBase);
return 0;
} আউটপুট
100