Dec 30, 2015 at 3:40pm UTC
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 Dec 30, 2015 at 7:58pm UTC
Dec 30, 2015 at 4:47pm UTC
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 Dec 30, 2015 at 7:25pm UTC
Dec 30, 2015 at 5:28pm UTC
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 Dec 30, 2015 at 5:36pm UTC
Dec 30, 2015 at 5:57pm UTC
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 Dec 30, 2015 at 5:58pm UTC
Dec 30, 2015 at 6:04pm UTC
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 Dec 30, 2015 at 6:07pm UTC
Dec 30, 2015 at 7:59pm UTC
Actually most of them are working guys , but i actually managed to understand the one that S G H had submitted ...