
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Convert a string representation of list into list in Python
- C++ Program to convert a number into a complex number
- 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++
- Golang Program to convert an integer into binary representation
- Convert negative denominator into positive:$\frac{5}{-3}$
- Binary representation of a given number in C++
- C++ Program to convert the string into a floatingpoint number
- Convert one base number system to another base system in MySQL
- How to convert a negative number to a positive one in JavaScript?
- Convert to Base -2 in C++
- C++ Representation of a Number in Powers of Other
- Golang Program to convert a number into a rational number
- Haskell Program to convert a number into a complex number
- Binary representation of next number in C++

Advertisements