Write a C program, called power of three.c, that prompts the user to input a pos
ID: 3623136 • Letter: W
Question
Write a C program, called power of three.c, that prompts the user to input a positive integer, and then checks whether this integer is a power of 3. If the input is, indeed, equal to 3^n for some non-negative integer n (for example 1 = 3^0, 81 = 3^4, or 129140163 = 3^17), the program should tell the user that the input is a power of 3 and print the exponent n. If the input is a positive integer which is not a power of 3, the program should print a corresponding message. Finally, if the input is an integer lessthan 1, the program should terminate with an appropriate error message. Here are sample runs of the program!
Enter a positive integer: 81
81 is a power of 3, exponent = 4
Enter a positive integer: 1
1 is a power of 3, exponent = 0
Enter a positive integer: 12
12 is not a power of 3!
Explanation / Answer
Println("Input Positive Integer") object Integer posint, temp, count; posint = input.NextInt(); temp = posint; if (posint < 1) { Println("Error, incorrect input - Integer is less than 1:); } else { while (temp mod 3 == 0) { temp = temp/3; count++; } if (posint == 1) { Println(posint + "is a power of 3, exponent = " + count); } else if (count > 0) { Println(posint + "is a power of 3, exponent = " + count); } else { Println(posint + "is not a power of 3"); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.