Question: In Java, write this program. This Ternary Base 3 program takes a positive integer that is inputted from the user and converted into ternary base

In Java, write this program.

This Ternary Base 3 program takes a positive integer that is inputted from the user and converted into ternary base 3. Write a decimal to a ternary converter. Assume the user enters a positive integer no bigger than 666. Read it and display the base-3 equivalent. (It will be helpful in the next program if you have somehow saved the digits, rather than simply calculating and printing them.)

0 0
1 1
2 2
3 10
27 1000

this is what I got so far. I'm not sure if it is in the right direction or not.

import java.util.Scanner;

public class Ternary_Based {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int TernaryAnswer = 0;

int input;

System.out.println("Enter input: ");

input = sc.nextInt();

sc.close();

while (input > 0) {

int factor = 1;

TernaryAnswer += input % 3 * factor;

input /= 3;

factor *= 10;

}

System.out.println(TernaryAnswer);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!