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 which comprises of digits **-1, 0 and 1**. Since it is inconvenient to write -1 as a digit, we’ll use 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 ternary number system.
- Convert ternary to balanced ternary system, using 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 238_{10} to balanced ternary and vice-versa

First convert 238

_{10}to ternary number system.238_{10}= 22211_{3}

Second convert ternary to balanced ternary number system :

- By starting iteration from
left to right,two 1’s are skippedas 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 of negative sign before the number. All negative numbers in balanced ternary system starts with Z. **i.e.: ****−1 _{10} = Z_{3}, **

**−2**

_{10}= Z1_{3},**−3**

_{10}= Z0_{3},**−4**

_{10}= ZZ_{3},**−5**

_{10}= Z11_{3}.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;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output:**

Equivalent Balanced Ternary of 238 is: 100Z11

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Ternary number system or Base 3 numbers
- Ternary representation of Cantor set
- Convert the number from Indian system to International system
- Convert the number from International system to Indian system
- Number of balanced parenthesis substrings
- Check if the number is balanced
- Check if N is a Balanced Prime number or not
- Print all combinations of balanced parentheses
- Count Balanced Binary Trees of Height h
- Balanced Prime
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Print the balanced bracket expression using given brackets
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Digitally balanced numbers
- Minimum Cost required to generate a balanced Bracket Sequence
- Count balanced nodes present in a binary tree
- Find n'th number in a number system with only 3 and 4
- Number of possible Triangles in a Cartesian coordinate system
- Number System and Base Conversions
- Largest Even and Odd N-digit numbers in Octal Number System

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.