Skip to content

Commit 518d699

Browse files
committed
program to count number fo consecutive ones
1 parent c3085cf commit 518d699

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<iostream>
2+
3+
4+
using namespace std;
5+
6+
/*Program to count number of consecutive ones using left shifting the number until it becomes 0
7+
*/
8+
9+
int countConsecutiveOnes(int n){
10+
11+
int count=0;
12+
while(n)
13+
{
14+
n &= n << 1;
15+
count++;
16+
}
17+
18+
return count;
19+
}
20+
21+
22+
int main()
23+
{
24+
cout<<countConsecutiveOnes(15);
25+
return 0;
26+
}

0 commit comments

Comments
 (0)