Thread: Program Involving Binary Numbers

1. Program Involving Binary Numbers

Here is the Q:
Find the maximum number of continuous zeros,in a given Binary Number.

Example:
Binary Number : 100010000000110
Output : 7

Binary Number : 1010001
Output : 3

Here is the code i developed so far:
(I use traditional C Code supported by Turbo C++ v3.0)

Code:
```/*Binary Numbers-Max Number of Zeros*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100];
int i,max,count;
clrscr();
printf("Enter the Binary Number\n");
gets(s1);
i=0;max=0;count=0;
while(s1[i]!='\0')
{
if(s1[i]=='0')
{
while(s1[i]=='0')
count++;
if(count>max)
max=count;
}
count=0;
i++;
}
printf("Maximum Number of Zeros=%d",max);
getch();
}```
where 'i' is used to keep track of the string s1,'count' is used to keep track of number of continuous zeros and 'max' stores the maximum value of zeros.

When I Execute this am getting correct answer only if entered binary number contains only 1s and becomes an infinite loop if the binary number contains any zeros.

Hoping for a Fast Reply

~FT

2. > while(s1[i]=='0')
> count++;
i is a constant in this loop.

3. Thanks for Your Reply
Fixed the program.

Fixed Code:

Code:
```/*Binary Numbers-Max Number of Zeros*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100];
int i,max,count;
clrscr();
printf("Enter the Binary Number\n");
gets(s1);
i=0;max=0;count=0;
while(s1[i]!='\0')
{
if(s1[i]=='0')
{
while(s1[i]=='0')
{
count++;i++;
}
if(count>max)
max=count;
}
count=0;
i++;
}
printf("Maximum Number of Zeros=%d",max);
getch();
}```

4. Now learn about void main, gets, and turboc
SourceForge.net: cpwiki

Popular pages Recent additions