a problem set by c++ and c


Write a program without any semicolons and it takes an input X from the user and then print "Hello" X times each "Hello" in a single line
Last edited on
Not possible. Every statement (and such a program must have at least one) must be terminated by a semicolon.

There are sleight-of-hand tricks, of course, which you can use to present a "source" that has no semicolons, but cheating doesn't count.

[edit] I forgot about using conditional statements...
Last edited on
Hmm. Maybe trickery like:
1
2
3
4
int main() {
    if( std::cin >> x ) {}
    if( std::cout << "text\n" ) {}
}

could help? A real weird challenge in any case.
It can be done. The trick is to bury the statements within if and while conditions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>

int main()
{
  if (int X = 1)
    if (scanf("%d", &X))
      {
	{	 
	  while (X--)
	    {
	      if (printf("Hello "))
		{}
	    }
	}
      }
}

Last edited on
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    if(int N=-1)
        while((--N > 0 || std::cin >> N) && (std::cout << "Hello World" << std::endl)) {}
}


This also loops on the std::cin (but inputting a negative number will treat it as a 1).
Also, if you print a letter, it softly breaks away from the loop.
Last edited on
each "Hello" in a single line

This requirement may be ambiguous, I interpreted it differently.
Meaning, each hello is on its own separate line, but there is no newline at either the start of end.
A simple tactic to replace expression statements would be to use the preprocessor, this solves having to type the if-statement a lot of times.

 
#define STMT(X) if((X),0) {} //The comma operator is used to make sure it always returns false 


Then use it like this:

 
STMT(std::cout << "Hello" << std::endl)


Note you that you cannot use this for variable declarations, since these will have a limited scope.
Last edited on
Actually most of them are working guys , but i actually managed to understand the one that S G H had submitted ...
Topic archived. No new replies allowed.