Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming language Please provide image of output Thank you! A binary gap wi

ID: 3913993 • Letter: C

Question

C programming language Please provide image of output Thank you!
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
int solution(int N); that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Assume that:
N is an integer within the range [1..2,147,483,647].
C programming language Please provide image of output Thank you!
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
int solution(int N); that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Assume that:
N is an integer within the range [1..2,147,483,647].
C programming language Please provide image of output Thank you!
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
int solution(int N); that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Assume that:
N is an integer within the range [1..2,147,483,647].

Explanation / Answer

#define _CRT_SECURE_NO_DEPRECATE

#include<stdio.h>

int solution(int N);

int main()

{

int n,gap;

printf("Enter the number: ");

scanf("%d", &n);

gap = solution(n);

if (gap > 0)

printf(" Number %d has binary gaps of length %d ", n,gap );

else

printf(" Number %d has no binary gaps ",n);

}

int solution(int N)

{

//declare an array to hold the binary digits,since max int value is of 10 digits ,declare array of int with size 10

int binary[10];  

int rem,count=0,i,j,k=0,found=0;

int longest_gap[10] = { 0 },max;

//first convert the integer N to binary and assign to the binary array

do

{

rem = N % 2;

binary[count++] = rem;

N /= 2;

} while (N >= 1);

//binary number is reverse of the array binary,, hence display from max index of count-1

printf("Binary number is : ");

for (i = count-1; i >= 0; i--)

{

printf("%d", binary[i]);

}

for (i = count - 1; i >= 0; i--)

{

if (binary[i] == 1)

{

for (j = i - 1; j >= 0; j--)

{

if (binary[j] == 0)

{

++longest_gap[k];

continue;

}

else

{

found = 1;

break;

}

}

if (found)

{

found = 0;

k++;

}

else

{

longest_gap[k] = 0;

}

}

}

max = longest_gap[0];

for (i = k - 1; i >= 0; i--)

{

if (max < longest_gap[i])

{

max = longest_gap[i];

}

}

if (max >= 1)

return max;

else

return 0;

}

/*output1

Enter the number: 9

Binary number is : 1001

Number 9 has binary gaps of length 2

-----------------------

output2

Enter the number: 20

Binary number is : 10100

Number 20 has binary gaps of length 1

--------------------------------

output3

Enter the number: 529

Binary number is : 1000010001

Number 529 has binary gaps of length 4

-----------------------------

output4

Enter the number: 15

Binary number is : 1111

Number 15 has no binary gaps

---------------------------

output4

Enter the number: 32

Binary number is : 100000

Number 32 has no binary gaps

*/