Mega Code Archive

 
Categories / C++ Tutorial / Operators Statements
 

Determines if a value is a multiple of X

#include <iostream> using namespace std; int multiple( int ); int main() {    int y;    cout << "Enter an integer between 1 and 32000: ";    cin >> y;    if ( multiple( y ) )       cout << y << " is a multiple of X" << endl;    else       cout << y << " is not a multiple of X" << endl;    return 0; } int multiple( int num ) {    int mask = 1, mult = 1;    for ( int i = 0; i < 10; i++, mask <<= 1 )       if ( ( num & mask ) != 0 ) {          mult = 0;          break;       }    return mult; }