C++ Program to check given number is Pernicious or not [DEVCPP/GCC]

PERNICIOUS NUMBER

pernicious number is a positive integer which has prime number of ones in its binary representation.

PROGRAM

// C++ Program to check given number is Pernicious or not

#include <iostream>
#include <bitset>
#include <cassert>

using namespace std;

bool isPrime(int num)
{
    if(num == 1)
        return false;
   
    for(int j=2 ; j<=(num/2) ; j++)
    {
        if(num % j == 0)
            return false;
    }
   
    return true;
}

int main()
{
    long num, count = 0;
   
    cout << "ENTER A NUMBER : ";
    cin>>num;
 
    assert(num > 0);
 
    string binary = bitset<8>(num).to_string();
   
    for(int i=0 ; i < binary.length(); i++)
    {
        if(binary[i] == '1')
            count++;
    }
   
    if(isPrime(count))
        cout<<endl<<num<<" IS A PERNICIOUS NUMBER";
    else
        cout<<endl<<num<<" IS NOT A PERNICIOUS NUMBER";
 
   return 0;
}

OUTPUT


FIND US ON FACEBOOK!