Question
Adjust the program so it also converts the decimal number to ternary. That is, it also converts the number in base 10 to base 3.
Adjust the program so it also converts the decimal number to ternary. That is, it also converts the number in base 10 to base 3.
First add a method decimalToTernary() that mimics the structure of the provided method decimalToBinary().
Call your method and output both the binary and ternary expansions. Sample Output:
This program converts a positive integer in base 10 to bases 2 and 3.
Enter a positive integer n: 10
You entered the base-10 number: 10
In base-2 your number is 1010
In base-3 your number is 101
a. i. Show your output giving the binary and ternary expansions for the number (in base 10): n = 2^4 * 3^4 = 144
Why does the number 144 in base 2 end in more zeros than the number in base 3??
b. By exploration, decide the validity of the following three hypotheses.
H1: i. A number is even if and only if its base 2 expansion ends in 0. T or F _ _ _ _ _
H2: ii. A number is divisible by 3 if and only if its base 3 expansion ends in 0. T or F _ _ _ _ _
H3: iii. A number is even if and only if the sum of its trits is even. T or F _ _ _ _ _
Example for H3:
30 is even. Its trits are 1010, and the sum of these digits is 2, which is even.
31 is odd. Its trits are 1011, and the sum of these digits is 3, which is odd.
Ungraded Challenge: Can you determine if a number is divisible by 3 just by looking at its bits (base-2 digits)?
starting code:
// // Created by Robin Carr //
#include
/* Function to convert a decimal number to a binary number */ // illustrates a while loop long decimalToBinary(long n) { int remainder; long binary = 0, i = 1;
while (n != 0) { remainder = n % 2; n = n / 2; binary = binary + (remainder * i); i = i * 10; } return binary; } // end of method
// add a method decimalToTernary here
/* Function to convert a decimal number to a ternary number */
int main() {
printf(" This program converts a positive integer in base 10 to bases 2 and 3. "); printf("Enter a positive integer n: "); long number_base10; // the number to convert from base 10 to base 2
scanf("%ld", &number_base10); printf("You entered the base-10 number: %ld ", number_base10);
long number_base2; number_base2 = decimalToBinary(number_base10); printf(" In base-2 your number is %ld", number_base2);
// Add code to find your number in base-3 and print out that result too.
return 0; } // end of main
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