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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started