Ternary number system or Base 3 numbers
Last Updated :
18 Nov, 2021
A number system can be considered as a mathematical notation of numbers using a set of digits or symbols. In simpler words, the number system is a method of representing numbers. Every number system is identified with the help of its base or radix.
For example, Binary, Octal, Decimal and Hexadecimal Number systems are used in microprocessor programming. In this article, one such number system is discussed.
Ternary Number System:
If the Base value of a number system is 3, then this representation is known as a ternary representation. The digits in this system are 0, 1, and 2.
There is also a number system called Balanced Ternary which comprises the digits −1, 0 and +1. The Ternary representation of a number is compact than of binary number system.
Steps to Convert Decimal to Ternary:
- Divide the number by 3.
- Get the integer quotient for the next iteration.
- Get the remainder for the ternary digit.
- Repeat the steps until the quotient is equal to 0.
For example: let N = 101. The following image illustrates the step by step conversion of 10110 to the base-3.
Below is the implementation for the Decimal to Binary and Vice-Versa:
C++
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
void convertToTernary( int N)
{
if (N == 0)
return ;
int x = N % 3;
N /= 3;
if (x < 0)
N += 1;
convertToTernary(N);
if (x < 0)
cout << x + (3 * -1);
else
cout << x;
}
void convert( int Decimal)
{
cout << "Ternary number of "
<< Decimal << " is: " ;
if (Decimal != 0) {
convertToTernary(Decimal);
}
else
cout << "0" << endl;
}
int main()
{
int Decimal = 2747;
convert(Decimal);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void convertToTernary( int N)
{
if (N == 0 )
return ;
int x = N % 3 ;
N /= 3 ;
if (x < 0 )
N += 1 ;
convertToTernary(N);
if (x < 0 )
System.out.print( x + ( 3 * - 1 ));
else
System.out.print( x);
}
static void convert( int Decimal)
{
System.out.print( "Ternary number of " +Decimal + " is: " );
if (Decimal != 0 ) {
convertToTernary(Decimal);
}
else
System.out.println( "0" );
}
public static void main (String[] args)
{
int Decimal = 2747 ;
convert(Decimal);
}
}
|
Python3
def convertToTernary(N):
if (N = = 0 ):
return ;
x = N % 3 ;
N / / = 3 ;
if (x < 0 ):
N + = 1 ;
convertToTernary(N);
if (x < 0 ):
print (x + ( 3 * - 1 ), end = "");
else :
print (x, end = "");
def convert(Decimal):
print ( "Ternary number of " , Decimal,
" is: " , end = "");
if (Decimal ! = 0 ):
convertToTernary(Decimal);
else :
print ( "0" , end = "");
if __name__ = = '__main__' :
Decimal = 2747 ;
convert(Decimal);
|
C#
using System;
class GFG
{
static void convertToTernary( int N)
{
if (N == 0)
return ;
int x = N % 3;
N /= 3;
if (x < 0)
N += 1;
convertToTernary(N);
if (x < 0)
Console.Write( x + (3 * -1));
else
Console.Write( x);
}
static void convert( int Decimal)
{
Console.Write( "Ternary number of " +Decimal + " is: " );
if (Decimal != 0) {
convertToTernary(Decimal);
}
else
Console.WriteLine( "0" );
}
public static void Main ( string [] args)
{
int Decimal = 2747;
convert(Decimal);
}
}
|
Javascript
<script>
function convertToTernary(N)
{
if (N == 0)
return ;
let x = N % 3;
N = parseInt(N / 3, 10);
if (x < 0)
N += 1;
convertToTernary(N);
if (x < 0)
document.write(x + (3 * -1));
else
document.write(x);
}
function convert(Decimal)
{
document.write( "Ternary number of " + Decimal + " is: " );
if (Decimal != 0) {
convertToTernary(Decimal);
}
else
document.write( "0" + "</br>" );
}
let Decimal = 2747;
convert(Decimal);
</script>
|
Output:
Ternary number of 2747 is: 10202202
Time Complexity: O(log3N)
Auxiliary Space: O(log3N)
Steps to Convert Ternary to Decimal:
- Connect each digit from the ternary number with its corresponding power of three.
- Multiply each digit with its corresponding power of three and Add up all of the numbers you got.
For example: let N = 10202. The following image illustrates the step by step conversion of 102023 to the base-10.
Below is the implementation for the Decimal to Binary and Vice-Versa:
C++
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
void convertToDecimal( int N)
{
cout << "Decimal number of "
<< N << " is: " ;
if (N != 0) {
int decimalNumber = 0,
i = 0, remainder;
while (N != 0) {
remainder = N % 10;
N /= 10;
decimalNumber += remainder
* pow (3, i);
++i;
}
cout << decimalNumber << endl;
}
else
cout << "0" << endl;
}
int main()
{
int Ternary = 10202202;
convertToDecimal(Ternary);
return 0;
}
|
Java
class GFG{
static void convertToDecimal( int N)
{
System.out.print( "Decimal number of " +
N + " is: " );
if (N != 0 )
{
int decimalNumber = 0 ,
i = 0 , remainder;
while (N != 0 )
{
remainder = N % 10 ;
N /= 10 ;
decimalNumber += remainder *
Math.pow( 3 , i);
++i;
}
System.out.print(decimalNumber + "\n" );
}
else
System.out.print( "0" + "\n" );
}
public static void main(String[] args)
{
int Ternary = 10202202 ;
convertToDecimal(Ternary);
}
}
|
Python3
import math;
def convertToDecimal(N):
print ( "Decimal number of" , N, "is:" , end = " " );
if (N ! = 0 ):
decimalNumber = 0 ;
i = 0 ;
remainder = 0 ;
while (N ! = 0 ):
remainder = N % 10 ;
N = N / / 10 ;
decimalNumber + = remainder * math. pow ( 3 , i);
i + = 1 ;
print (decimalNumber);
else :
print ( "0" );
Ternary = 10202202 ;
convertToDecimal(Ternary);
|
C#
using System;
class GFG{
static void convertToDecimal( int N)
{
Console.Write( "Decimal number of " +
N + " is: " );
if (N != 0)
{
int decimalNumber = 0;
int i = 0, remainder;
while (N != 0)
{
remainder = N % 10;
N /= 10;
decimalNumber += remainder *
( int )Math.Pow(3, i);
++i;
}
Console.Write(decimalNumber + "\n" );
}
else
Console.Write( "0" + "\n" );
}
public static void Main()
{
int Ternary = 10202202;
convertToDecimal(Ternary);
}
}
|
Javascript
<script>
function convertToDecimal(N)
{
document.write( "Decimal number of " +
N + " is: " );
if (N != 0)
{
let decimalNumber = 0;
let i = 0, remainder;
while (N != 0)
{
remainder = N % 10;
N = parseInt(N / 10, 10);
decimalNumber += remainder * Math.pow(3, i);
++i;
}
document.write(decimalNumber + "</br>" );
}
else
document.write( "0" + "</br>" );
}
let Ternary = 10202202;
convertToDecimal(Ternary);
</script>
|
Output:
Decimal number of 10202202 is: 2747
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...