Balanced Ternary Number System
Last Updated :
12 Jul, 2025
As we already know, a Binary number system is a number system that has only 2 digits in it, i.e. 0 and 1. Similarly, we also know that a Ternary number system is a number system that has only 3 digits in it, i.e. 0, 1, and 2. In this article, we will learn about Balanced Ternary Number System.
A balanced ternary number system is a numeral system that comprises digits -1, 0, and 1. Since it is inconvenient to write -1 as a digit, we'll use the letter Z further for this purpose.
Conversion of Decimal to Balanced Ternary system
The conversion from Decimal to balanced ternary is done in two steps:
- Convert decimal to the ternary number system.
- Convert ternary to the balanced ternary system, using the below steps:
- traverse the ternary number, right to left by leaving 0 and 1 as it is
- when encounter 2, change it to Z and add +1 to the next digit in iteration.
- Some digits may become +3, then replace +3 with 0 and add +1 to next digit in iteration.
- complete this process until you convert all the digits.
Example: convert 23810 to balanced ternary and vice-versa
First convert 23810 to ternary number system.
23810 = 222113
Second convert ternary to balanced ternary number system :
- By starting iteration from right to left, two 1's are skipped as it remains same in balanced ternary.
- Now convert first encountered 2 with z increasing it's next digit in iteration by +1. So we get 23Z11.
- Convert 3 to 0 with increment +1 in it's next digit in iteration. So we get 30Z11.
- Convert 3 to 0 with increment +1 in it's next digit in iteration. So we get 100Z11. (Here assume 0 is present before most significant digit)
The final result is 100Z11.
Note: The system also allows representation of negative numbers eliminating the need for a negative sign before the number. All negative numbers in a balanced ternary system start with Z. i.e.
?110 = Z3,
?210 = Z13,
?310 = Z03,
?410 = ZZ3,
?510 = Z113
Below is the program to convert positive decimals into the balanced ternary system:
C++
// C++ program to convert positive
// decimals into balanced ternary system
#include <bits/stdc++.h>
using namespace std;
string balancedTernary(int n)
{
string output = "";
while (n > 0) {
int rem = n % 3;
n = n / 3;
if (rem == 2) {
rem = -1;
n++;
}
output = (rem == 0 ? '0' : (rem == 1) ? '1' : 'Z')
+ output;
}
return output;
}
// Driver code
int main()
{
int n = 238;
cout << "Equivalent Balanced Ternary of " << n
<< " is: " << balancedTernary(n);
return 0;
}
Java
// Java program to convert positive
// decimals into balanced ternary system
class GFG{
static String balancedTernary(int n)
{
String output = "";
while (n > 0)
{
int rem = n % 3;
n = n / 3;
if (rem == 2)
{
rem = -1;
n++;
}
output = (rem == 0 ? '0' :
(rem == 1) ? '1' : 'Z') + output;
}
return output;
}
// Driver code
public static void main(String[] args)
{
int n = 238;
System.out.print("Equivalent Balanced Ternary of " +
n + " is: " + balancedTernary(n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to convert positive
# decimals into balanced ternary system
def balancedTernary(n):
output = ""
while(n > 0):
rem = n % 3
n = n // 3
if(rem == 2):
rem = -1
n += 1
if(rem == 0):
output = '0' + output
else:
if(rem == 1):
output = '1' + output
else:
output = 'Z' + output
return output
# Driver Code
n = 238
# Function call
print("Equivalent Balanced Ternary of",
n , "is:", balancedTernary(n))
# This code is contributed by Shivam Singh
C#
// C# program to convert positive
// decimals into balanced ternary system
using System;
using System.Collections.Generic;
class GFG{
static String balancedTernary(int n)
{
String output = "";
while (n > 0)
{
int rem = n % 3;
n = n / 3;
if (rem == 2)
{
rem = -1;
n++;
}
output = (rem == 0 ? '0' :
(rem == 1) ? '1' : 'Z') + output;
}
return output;
}
// Driver code
public static void Main(String[] args)
{
int n = 238;
Console.Write("Equivalent Balanced Ternary of " +
n + " is: " + balancedTernary(n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to convert positive
// decimals into balanced ternary system
function balancedTernary(n)
{
var output = "";
while (n > 0) {
var rem = n % 3;
n = parseInt(n / 3);
if (rem == 2) {
rem = -1;
n++;
}
output = (rem == 0
? '0'
: (rem == 1)
? '1'
: 'Z')
+ output;
}
return output;
}
// Driver code
var n = 238;
document.write( "Equivalent Balanced Ternary of "
+ n + " is: "
+ balancedTernary(n));
</script>
Output: Equivalent Balanced Ternary of 238 is: 100Z11
Time Complexity: O(log3n)
Auxiliary Space: O(log3n)
Similar Reads
Digitally balanced numbers Given two integers N and B, the task is to check N is a Digitally balanced number in base B. Digitally balanced numbers in the base B are those numbers that have the same number of (0, 1, 2 ....B-1) digits in base B. Examples: Input: N = 9, B = 2 Output: Yes Equivalent binary number of 9 is 1001, wh
5 min read
Digitally balanced numbers Given two integers N and B, the task is to check N is a Digitally balanced number in base B. Digitally balanced numbers in the base B are those numbers that have the same number of (0, 1, 2 ....B-1) digits in base B. Examples: Input: N = 9, B = 2 Output: Yes Equivalent binary number of 9 is 1001, wh
5 min read
Number System and Base Conversions Electronic and digital systems use various number systems such as Decimal, Binary, Hexadecimal and Octal, which are essential in computing. Binary (base-2) is the foundation of digital systems.Hexadecimal (base-16) and Octal (base-8) are commonly used to simplify the representation of binary data. T
9 min read
Summation of Alphanumeric Strings Just like our number system where we have 10 digits starting from 0 to 9, Geek has created a custom numerical system where he has 36 digits which are 0 to 9 then A to Z where '0' is the smallest, and 'Z' is the largest number, which follows their respective order (Similar to Hexadecimal number syste
11 min read
Balance pans using given weights that are powers of a number Given a simple weighting scale with two pans, we are given a weight T and some other weights which are the powers of a specific number a, our goal is to balance these pans using the given weights. More formally we need to satisfy this equation, T + (some power of a) = (some other powers of a) Rememb
10 min read
Make the triplet same by adding 2, 4, and 6 to the given numbers Given three integers x1, x2, and x3, we can perform an operation that simultaneously adds 2 to one of them, adds 4 to another, and adds 6 to the third. We can perform this operation any number of times on any permutation of the indices (1, 2, 3), the task is to determine whether it is possible to ma
7 min read