C++ Online Compiler
Example: Decimal to Base 9 Conversion in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Decimal to Base 9 Conversion #include <iostream> #include <string> // Required for std::string #include <algorithm> // Required for std::reverse int main() { // Step 1: Declare variables int decimalNum; std::string base9Num = ""; // To store the base 9 representation // Step 2: Prompt user for input std::cout << "Enter a decimal number: "; std::cin >> decimalNum; // Handle the special case for 0 if (decimalNum == 0) { base9Num = "0"; } else { // Step 3: Perform repeated division int tempNum = decimalNum; while (tempNum > 0) { int remainder = tempNum % 9; // Get the remainder base9Num += std::to_string(remainder); // Convert remainder to string and append tempNum /= 9; // Divide the number by 9 } // Step 4: Reverse the collected remainders std::reverse(base9Num.begin(), base9Num.end()); } // Step 5: Display the result std::cout << "Decimal " << decimalNum << " in Base 9 is: " << base9Num << std::endl; return 0; }
Output
Clear
ADVERTISEMENTS