Write a C++ program to check whether a given number is a power of two or not

  • برمجة سي بلس بلس
  • برمجة

Write a C++ program to check whether a given number is a power of two or not

Sample Output:

Is 8 is power of 2: True
Is 256 is power of 2: True
Is 124 is power of 2: False

الأجوبة

#include <iostream>
#include <cmath>
using namespace std;
string Powers_of_Two(int n) {

	for (int x = 0; x < INT_MAX; x++)
	{
		if (pow(2, x) == n)
		{
			return "True";
		}
		else if (pow(2, x) > n)
		{
			break;
		}
	}

	return "False";
}
int main() {
	cout << "Is 8 is power of 2: " << Powers_of_Two(8) << endl;
	cout << "Is 256 is power of 2: " << Powers_of_Two(256) << endl;
	cout << "Is 124 is power of 2: " << Powers_of_Two(124) << endl;
	return 0;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...