Write a C program to take input in binary and add 1 in the given value.
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[25];
int b[25];
int i,count=0,z=48,o=49,j=0,debug=0; //here i intailze z means zero to 48 bcz acii value is 48 and o means one 49 in asII
/*Input*/
printf("Enter binary number:");
gets(a);
/* actual Working */
for(i=0;a[i]!='\0';i++) //this loop will count total elements in array
{
count=count+1;
}
count=count-1;
if(a[count]==z) //here i cheak array a and index count-1 is equal to zero so this condition will run
{
a[count]=o; //here a[count] set one
for(i=0;a[i]!='\0';i++)
{
printf("%c",a[i]);
}
}
else if(a[count]==o && a[count-1]==z) //if a[count] equal to one and a[count-1] equal to zero
{
a[count]=z;
if(a[count-1]==z)
{
a[count-1]=o;
for(i=0;a[i]!='\0';i++)
{
printf("%c",a[i]);
}
}
}
else if(a[count]==o && a[count-1]==o)
{
a[count]=z;
count=count-1;
if(a[count]==o)
{
a[count]=z;
}
if(a[count-1]=z)
{
a[count-1]=o;
}
debug=1;
}
if(debug==0)
{
for(j=8;a[j]!='\0';j++)
{
if(a[j]>=48 && a[j]<=57)
{
printf("%c",a[j]);
}
}
}
else
{
for(j=0;a[j]!='\0';j++)
{
printf("%c",a[j]);
}
}
return 0;
}
PROGRAM OUTPUT:
For more problems click here
No comments:
Post a Comment