Skip to content
Related Articles

Related Articles

Improve Article
Condition To Print “HelloWord”
  • Difficulty Level : Easy
  • Last Updated : 29 May, 2018

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

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

Method 1:




#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



Method 2: Using fork()




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

This method is contributed by Aravind Alapati.

Please comment if you find more solutions of this.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :