//This program takes a binary string and converts it into the 2s complement. //
#include<stdio.h>
#include<string.h>
#include<conio.h>
void revert(char *str,char *comp,int l);
int main()
{
int i,found=0,l,stlen;
char *str,*comp;
clrscr();
printf("
Enter a binary string
");
gets(str);
stlen=strlen(str);
l=stlen;
l--;
// printf("
length=%d..%c..",l,str[l]);
while(l)
{
if(found)
{
revert(str,comp,l);
break;
}
else
{
if(str[l]==1)
found=1;
comp[l]=str[l];
printf("%c",str[l]);
l--;
}
}
printf("
copmlement is");
puts(comp);
getch();
return(0);
}
void revert(char *str,char *comp,int l)
{
while(l>=0)
{
if(str[l]==1)
{
printf("0");
comp[l--]=0 ;}
else
{
printf("1");
comp[l--]=1;}
}
}
#include<stdio.h>
#include<string.h>
#include<conio.h>
void revert(char *str,char *comp,int l);
int main()
{
int i,found=0,l,stlen;
char *str,*comp;
clrscr();
printf("
Enter a binary string
");
gets(str);
stlen=strlen(str);
l=stlen;
l--;
// printf("
length=%d..%c..",l,str[l]);
while(l)
{
if(found)
{
revert(str,comp,l);
break;
}
else
{
if(str[l]==1)
found=1;
comp[l]=str[l];
printf("%c",str[l]);
l--;
}
}
printf("
copmlement is");
puts(comp);
getch();
return(0);
}
void revert(char *str,char *comp,int l)
{
while(l>=0)
{
if(str[l]==1)
{
printf("0");
comp[l--]=0 ;}
else
{
printf("1");
comp[l--]=1;}
}
}
0 komentar:
Posting Komentar