Thread: Program Involving Binary Numbers

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    Question 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while(s1[i]=='0')
    > count++;
    i is a constant in this loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Now learn about void main, gets, and turboc
    SourceForge.net: cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-23-2011, 01:01 PM
  2. Help with poker program(involving structs) C
    By lilbo4231 in forum C Programming
    Replies: 1
    Last Post: 04-25-2011, 11:43 PM
  3. Replies: 3
    Last Post: 09-08-2010, 10:26 AM
  4. array involving even numbers
    By dantestwin in forum C++ Programming
    Replies: 13
    Last Post: 07-10-2004, 08:40 AM
  5. Replies: 2
    Last Post: 03-13-2003, 09:40 AM

Tags for this Thread