C Printf Puzzle 1 : Guess value of X : “Hello World”
In this example we need to find the value of X in such a way that we should get “Hello World” as output
C programming puzzle
#include<stdio.h> void main() { if(X) { printf("Hello"); } else { printf("World"); } }
What would be value of X in order to get Hello World
as output ?
Different ways to solve c programming puzzle
Way 1 : Use of printf statement
#include<stdio.h> void main() { if(printf("Hello")-5) { printf("Hello"); } else { printf("World"); } }
Explanation of the Program :
- Printf Returns Number of Characters Printed On Screen
- Printf Inside “If Statement” will print “Hello” and returns Integer(number of Charcaters Printed)
- Subtract 5 from That number so We will get if(0) which is False Condition.
- Inside “else” block we are printing “world” .
Way 2 : Output :
#include<stdio.h> void main() { if(!printf("Hello")) { printf("Hello"); } else { printf("World"); } }
Way 3 : Output :
#include<stdio.h> void main() { if((printf("Hello") > 0 ? 0 : 1)) { printf("Hello"); } else { printf("World"); } }