Q:

Write a C++ programming to check if a given integer is a power of three or not

0

Write a C++ programming to check if a given integer is a power of three or not

Input: 9
Output: true
Input: 81
Output: true
Input: 45
Output: false

Sample Output:

If 15 is power of three? False

If 9 is power of three? True

If 243 is power of three? True

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;

string is_PowerOf_Three(int n) {
        while (n % 3 == 0) {
            n = n / 3;
        }

        if (1 == n) {
            return "True";
        } else {
            return "False";
        }
    }

int main(void)
{
    int n = 15;
    cout << "\nIf " << n << " is power of three? " << is_PowerOf_Three(n) << endl; 
    n = 9;
    cout << "\nIf " << n << " is power of three? " << is_PowerOf_Three(n) << endl; 
    n = 243;
	cout << "\nIf " << n << " is power of three? " << is_PowerOf_Three(n) << endl; 
    return 0;   
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now