# [C] A Few Challenging Questions

#### Moonwraith

A friend of mine stumbled across the following site:

http://www.gowrikumar.com/programming/index.html

It's basically made up of questions that are meant to get you into the right state of mind for an interview, apparently. Although, most of them so far have just confused the sweet jesus out of me.

There's one that's been baffling us for a good few hours now...

7.Write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.

If anyone can think of a decent answer to this, then you're a lot better than me (not hard).

Quite a few fun questions on there mind you. Ideally one day I'm thinking of working through all of them. Couple of other ones currently sucking are: question 3 and figuring out all possible ways to solve 8, currently know of two, but apparently there's at least three.

7.Write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement.

Code:
``````int onepern (int n)
{
fprintf (stdout,"1 to %i one per each line\n",n);
return 0;
}``````

C is a tool of the devil.

A better solution?

Create a really long string that contians all the numbers from 0 - MAXINT (32567) seperated by newlines (\n)

char *INTNUMBRS = "1\n2\n3\n4\n5 ... \n32567\n"

Create an array from 0-MAXINT. The values in each array being the chracter position where the Nth number is

eg:

INTARRAY[0] = 1;
INTARRAY[1] = 3;
INTARRAY[1] = 5;

etc

Find the Nth + 1 chracter in the INTARRAY, and just before that position insert the null
terminatator \0

NPos = INTARAY[N + 1];
INTNUMBERS[NPos - 1] = '\0';

print out INTNUMBERS to stdout.

Optionally, instead of using the array, you could probably come up with a match function
that found the Nth chracter in the string. I'm just too tired to do the math for it.

If it's a trick question, and I got that in an interview, I'd be totally pissed off 😛

Wonder if there _is_ an elegant solution out there

Why would you say it is a "trick" question? I don't see any trick to it?

### Members online

No members online now.

### Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Impressed you have kept this alive this long EP! So many sites have come and gone. 🙁

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.