C-program to find binary number for given decimal number and its one’s complement and two’s complement
#include<stdio.h>
#include<conio.h>
void main(){
int n,h,r,count=0,c=0,tem[10],b[10],o[10],t[10],i,j=0;
clrscr();
printf("Enter the Decimal Number: ");
scanf("%d",&n);
h=n;
printf("\n The Binary number for %d is: ",h);
i=0;
while(n>0){
r=n%2;
tem[i]=r;
i++;
c++;
n=n/2;
}
for(i=c-1;i>=0;i--){
b[j]=tem[i];
j++;
}
for(i=0;i<j;i++){
printf("%d",b[i]);
}
printf("\n 1’s com of %d is : ",h);
for(i=0;i<j;i++){
if(b[i]==0)
o[i]=1;
else
o[i]=0;
}
for(i=0;i<j;i++)
printf("%d",o[i]);
printf("\n Tow's complement of %d is:",h);
for(i=j-1;i>=0;i--){
if(count==0 && b[i]==1){
t[i]=1;
count++;
}
else if(count==0 && b[i]==0){
t[i]=b[i];
}
else
{
if(b[i]==1) t[i]=0;
else  t[i]=1;
}//else
}//for
for(i=0;i<j;i++)
printf("%d",t[i]);
getch();
}
Output:




0 comments: