One of my friend was asked in his interview to WRITE A ONE LINE PROGRAM TO ACCEPT A NUMBER FROM USER AND PRINT THE SAME. The main condition is to write the program in one line(i.e., only in one statement)only. Is there any way to write a program in that manner. If yes, then pls show me the code in reply.
One line? In C? Write program, remove line returns. Done. The C compiler does not care about how many lines there are, just the semicolons. One statement? It's (virtually) impossible to write a valid C program to do the same without more than one semicolon (and certainly not portably), so no. You can do some horrible macro trick most probably but that's really just cheating about the definition "statement".
If they didn't specify the language, in BASIC it's even easier, and in things like Perl it's a cinch if you know the language.
That said, it's a fecking stupid question that should never have been asked. You do *NOT* want to work for the person who asked that, and I'd have told them so in the interview. Something like "Sorry, but I only write good, well-documented code for real-world problems, not unnecessarily obfuscated logic tricks and trivia that would be non-portable and impossible for others to continue my work on." Having "odd" interview questions is about determining how you think, not what answer you give, but some interviewers have missed the point and think that asking the same questions as Google is "good interview technique". It's not. What you want to know is *HOW* they would go about solving a seemingly-impossible problem, not what their answer is, or even if they could find an answer. And guess what answer a person in charge of a team of professional programmers would want to hear about such a stupid, artificial restriction that could only result in unreadable, unportable code? "Sorry, but I only write..."
I could do something like that with a single statement in C++.
But am not sure how portable it is..or whether the same can be done in C.
The statement is:
Except that's not a valid *program*. For a start, there's no entrypoint, no definition of std (because you haven't included iostream), etc. etc.
Originally Posted by manasij7479
There's a vast difference between a program and a statement. The OP said a one-line program.
It's not hard to do in C or C++, as long as you keep in mind that declarations are not statements. There are several ways to write single statements with multiple effects. And the easiest way, in C or C++, to obtain user input is from the command line ;)
However, I agree with ledow. If I was asked such a question in an interview, I would ask if they expect such code to be written in a production environment. If they do expect such code, or don't actually answer my question, then I would apologise for wasting their time and end the interview.
And the first post included the phrase : "(i.e., only in one statement)"
Originally Posted by ledow
Do verify your points before jumping up to nitpick.
As well as saying:
"WRITE A ONE LINE PROGRAM" and "The main condition is to write the program in one line"
But who cares. The statement is easy (because you can just assume that all headers and boilerplate are included), the program is all-but impossible without some horrendously obfuscated macro tricks, I imagine.