Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Solution to homework assignment 2
- * Introduction to programming course
- * Faculty of Mathematics and Informatics of Sofia University
- * Winter semester 2021/2022
- *
- * @author Monika Dobrinova
- * @idnumber 8MI0600008
- * @task 1
- * @compiler vs
- *
- */
- #include <iostream>
- using namespace std;
- bool hasAlternatingBits(unsigned n) {
- bool True_or_False = true;
- int i, Binary[10];
- for (i = 0; n > 0; i++)
- {
- Binary[i] = n % 2;
- n = n / 2;
- }
- for (i = i - 1; i >= 0; i--)
- {
- if (Binary[i] == Binary[i + 1])
- {
- True_or_False = false;
- break;
- }
- else
- {
- True_or_False = true;
- }
- }
- if (True_or_False == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- int main() {
- int n;
- cin >> n;
- int Binary[10];
- if ( n >= 0 )
- {
- if (hasAlternatingBits(n) == true)
- {
- cout << "True";
- }
- else if (hasAlternatingBits(n) == false)
- {
- cout << "False";
- }
- }
- else
- {
- cout << "-1";
- }
- }
Add Comment
Please, Sign In to add comment