Friday, July 11, 2008

to find whether a number is in power of 2 or not.

/*solution in C language*/

int powerof2(int num) /*return 1 if number is in power of 2 otherwise 0*/
{
return !(num & num-1);
}

Find the sum of a series. S = 1-3+5-7+9 ... n

/*in c language */
int s=0;
int sign=1;
int i;
for(i=1; i<=n; i+=2)
{
s = s+ sign*n;
sign = -sign;
}

/*s will contain the sum of the series*/