Saturday 11 November 2017

C Program to Find the Position of String of 1-bits in a Number for a given Length


Code:

#include  stdio.h

void main()
{
    int n, len, pos = 0, i = 0, count = 0;

    printf("**Finding the position of 1-bits in a number for given length**\n");
    printf("enter a number\n");
    scanf("%d", &n);
    printf("enter the length\n");
    scanf("%d", &len);
    while (i <= 32)
    {
        if ((n & 1) ==  1)    //checking whether there is a 1-bit in the current position
        {
            count++;//counting the consecutive 1's in the integer
            pos = i;
            if (count ==  len)    //checking whether the length matches
            {
                break;
            }
        }
        if ((n & 1) ==  0)
        {
            count = 0;
        }
        n = n>>1;
        i++;
    }
    printf("the position of 1 in the string : %d\n", pos);
}


Output:

**Finding the position of 1-bits in a number for given length**
enter a number
10000
enter the length
3
the position of 1 in the string : 10
$ a.out
enter a number
700
enter the length
4
the position of 1 in the string : 5


More C Programs:






100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...