#include<stdio.h>
#include<stdlib.h>
/* Fnction to check if n is a multiple of 3*/
int isMultipleOf3(int n)
{
int oddD = 0, evenD = 0;
n = abs(n);
if(n ==0) return 1;
else if( n == 1 || n == 2) return 0;
else{
while(n != 0)
{
if(n & 1)
oddD++;
n = n>>1;
if(n & 1)
evenD++;
n = n>>1;
}
return isMultipleOf3(oddD - evenD);
}
}
/* Program to test function isMultipleOf3 */
int main()
{
int num = 222;
if (isMultipleOf3(num))
printf("num is multiple of 3");
else
printf("num is not a multiple of 3");
return 0;
}
Comments
Post a Comment