0
/*following is the shortest program that finds whether the entered no. is power of two or not,but i need to add some code(logic) to it.

 

note:- program do not make use of keywords like if ,while ,for ,switch. */

 

#include<stdio.h>

void main()

{

 int n;

 char c[2][23]={"no. is not power of 2","no. is power of 2"};

 scanf("%d",&n);

 printf("%s",c[/*add code*/]);

 }

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

can anyone tell me the code i should write in printf statement so as to know that whether the number is power of 2 or not

5
Contributors
9
Replies
10
Views
12 Years
Discussion Span
Last Post by EsMaru
0

inside the add code... put

((n & 1)+1)%2

or if u are allowed to change the char string... swap those two around and u only need

(n & 1)

0

inside the add code... put

((n & 1)+1)%2

Without context I might then do...

if ( ((num & 1)+1)%2 )
      {
         printf("num = %d\n", num);
      }

Which is horribly wrong.

or if u are allowed to change the char string... swap those two around and u only need

(n & 1)

Again, same reply.

Please stop trying to make correct code incorrect. Or at least attempt to provide enough code to prove your point.

0

I'm not correcting his code or anything... my stuff is new... just copy paste what i wrote and replace /*add code*/...

as in...

/*following is the shortest program that finds whether the entered no. is power of two or not,but i need to add some code(logic) to it.

note:- program do not make use of keywords like if ,while ,for ,switch. */

#include<stdio.h>

void main()

{

int n;

char c[2][23]={"no. is not power of 2","no. is power of 2"};

scanf("%d",&n);

printf("%s",c[((n & 1)+1)%2]);

}

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.