

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a number into negative base representation in C++
In this tutorial, we will be discussing a program to convert a number into its negative base representation.
For this we will be provided with a number and the corresponding negative base. Our task is to convert the given number into its negative base equivalent. We are allowing only values between -2 and -10 for negative base values.
Example
#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; }
Output
100
- Related Questions & Answers
- Convert a string representation of list into list in Python
- Golang Program to convert an integer into binary representation
- Is it possible to convert a number into other base forms using toString() method in JavaScript?
- Convert one base number system to another base system in MySQL
- How to convert a negative number to a positive one in JavaScript?
- Converting numbers to base-7 representation in JavaScript
- How to convert a string into number in PHP?
- Sum a negative number (negative and positive digits) - JavaScript
- Find the Number of Trailing Zeroes in Base 16 Representation of N! using C++
- Find the Number of Trailing Zeroes in base B Representation of N! using C++
- MySQL query to convert a string into a month (Number)?
- Convert BigInteger into another radix number in Java
- Binary representation of a given number in C++
- C++ Pandigital Number in a Given Base
- Convert to Base -2 in C++
Advertisements