C Programming – HackerRank Solution | Right most set bit |

(HackerRank) Write a Modular C Programming code to solve Right most set bit, Given a non-negative number find the position of the right – most set bit (1)

Given a non-negative number find the position of the right – most set bit (1) in the binary representation of the number.

Input Format

First line contains a non-negative number.

Constraints

N>0 and N<= (2^31-1)

Output Format

Display the bit number, where the right most set bit is found in a single line.

Sample Input 0

```8
```

Sample Output 0

```3
```

Explanation 0

``````8 in binary representation is 1000. We clearly see that the set bit from RHS is at position 3. (right most bit is at position 0.)

3 2 1 0 -----> Bit position
1 0 0 0 -----> The bits
``````

Sample Input 1

```-9
```

Sample Output 1

`Invalid`

CODE:

```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main() {
int n,b;
scanf("%d",&n);
if(n>0)
{
int i=0;
while(n!=0)
{
b=(n%2)*pow(10,i);
if(n%2==1)
break;
n=n/2;
i++;
}
printf("%d",i);
}
else{
printf("Invalid");
}
return 0;
}

```

OUTPUT

```Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

8

3
Expected Output

3```

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University