Ternary number system or Base 3 numbers | C ++ program to convert a ternary number to decimal number ; Function to convert a ternary number to a decimal number ; If the number is greater than 0 , compute the decimal representation of the number ; Loop to iterate through the number ; Computing the decimal digit ; Driver code
#include <cstdio> NEW_LINE #include <iostream> NEW_LINE #include <math.h> NEW_LINE 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 ; }