Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 47916df

Browse files
Added C program to find the closest power of 2 for a number
1 parent a56b839 commit 47916df

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎closestpowerof2.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Program for printing closest of 2 of a number.
2+
3+
#include<stdio.h>
4+
#include<math.h>
5+
6+
int main()
7+
{
8+
int num,a,b,c,d=0;
9+
scanf("%d",&num);
10+
for(b=31;b>=0;--b)
11+
{
12+
c=num>>b;
13+
if(c&1)
14+
{
15+
d++;
16+
if(d>1)
17+
{
18+
num=num-pow(2,b);
19+
}
20+
}
21+
}
22+
printf("%d",num);
23+
return 0;
24+
}
25+
//not working for negative no.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /