Condition To Print “HelloWord”

2.3

What should be the “condition” so that the following code snippet prints both HelloWorld !

      if  "condition"
          printf ("Hello");
      else
          printf("World"); 

Solution:

#include<stdio.h>
int main()
{
    if(!printf("Hello"))
        printf("Hello");
    else
        printf("World");
    getchar();
}        

Explanation: Printf returns the number of character it has printed successfully. So, following solutions will also work

if (printf(“Hello”) < 0) or if (printf("Hello") < 1) etc Please comment if you find more solutions of this.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



2.3 Average Difficulty : 2.3/5.0
Based on 29 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.