The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Sunday, April 1, 2012

1's Complement of a Number



#include<stdio.h>
#include<math.h>
#include<conio.h>

 void main()
  {
int n;
scanf("%d",&n);
int bin[20];    // Binary Equivalent Number of 'n'
int k = 0;
int i;
while(n != 0)
{
 bin[k] = n % 2;    // Computing Binary Equivalent Number of 'n' in Reverse Order
 k++;
 n = n/2 ;
}

 printf("\n\n");
for(i=k-1;i>=0;i--)
 printf(" %d",bin[i]);

for(i=0;i<k;i++)
 bin[i] = (bin[i]==1)?0:1;  // Complementing Binary Equivalent Number of 'n'

printf("\n\n");
for(i=k-1;i>=0;i--)
 printf(" %d",bin[i]);

i = 0;
n = 0;
int prod = 1;

while(i<k)
{
 printf(" %d",prod);
 n = n + (bin[i]*prod);               // Computing 'n' from Binary Equivalent Number
 prod *= 2;

 ++i;
}
printf("\n %d",n);

getch();
}

No comments:

Post a Comment