Like Tree3Likes
  • 2 Post By Salem
  • 1 Post By Salem

Program Involving Binary Numbers

This is a discussion on Program Involving Binary Numbers within the C Programming forums, part of the General Programming Boards category; Here is the Q: Find the maximum number of continuous zeros,in a given Binary Number. Example: Binary Number : 100010000000110 ...

  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
    32,845
    > while(s1[i]=='0')
    > count++;
    i is a constant in this loop.
    Abhishek Sp and iMalc like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,845
    Now learn about void main, gets, and turboc
    SourceForge.net: cpwiki
    Abhishek Sp likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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-26-2011, 12:43 AM
  3. Replies: 3
    Last Post: 09-08-2010, 11:26 AM
  4. array involving even numbers
    By dantestwin in forum C++ Programming
    Replies: 13
    Last Post: 07-10-2004, 09:40 AM
  5. Replies: 2
    Last Post: 03-13-2003, 09:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21