Puzzles

K

Kevin Handy

Girish said:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.

Ok. 'a "Hello World" program in 'C' without using a semicolon'
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;

int main()
{
cout << "numbers from 1 to 100 and 100 to 1;" << endl;
}

Why are you asking C++ questions in a C newsgroup?
3. Find if the given number is a power of 2.

What's the given number?
4. Multiply x by 7 without using multiplication (*) operator.

y = pow(10.0, log10(x)+log10(7));
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7

char* a_function_in_different_ways()
{
return "f(7) = 4 and f(4) = 7";
}
6. Remove duplicates in array

for (loop = 0; loop <= elements; loop++)
array[loop] = 0;
elements = 0;

It also has the added feature of removing the non-duplicates too.
7. Finding if there is any loop inside linked list.

trace = first
while(trace)
{
if (trace.value = loop)
printf("Here it is!");
trace=trace.next;
}
8. Remove duplicates in an no key access database without using an
array

Just delete everything.

It also has the added benefit of removing non-duplicates too.
9. Convert (integer) number in binary without loops.

convert("(integer) number in binary without loop");

Are we supposed to rot13 it, or what?
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.

int main()
{
printf("an exact copy of the source.\n");
}
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.

So?

No computer in this question. Only several people playing
in the water.
13. Swap two numbers without using a third variable.

temp = a;
a = b;
b = temp;

See, i never used "a third variable" in that code sequence.
I guess you could

temp2 = "a third variable"
temp = a;
a = b;
b = temp;

but it just adds bloat. Or is it supposed to be in a comment?


Do I win any prizes?
 
J

Jens Schicke

Girish Pal Singh said:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.

#define HEL "4$"
#define LO "c%"
#define WOR "s%2"
#define LD "$"
#define _ LD LO
#define HELLO HEL LO
#define WORLD "1"

void main(void)
{
if(printf("%"LO HELLO WORLD HEL WOR WORLD _"2"_ WORLD"3"LD"s%5"LD"s%" WORLD HEL WOR HELLO"28"LD"c",
'H', 'o', 'm', 'e', "Wor", 'k', ' ',
's', 'u', 'c', 'k', 's', " ", "l", 'i', "k", 'e',' ',
'h', 'e', 'l', 'l', ' ', 'd', "u", "d", 'e', '\n'))
{
}
}
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;

int dargc = 1;

int main(int argc, char* argv[])
{
return argc ?
printf("%d\n", argc),
((argc == 100) ?
printf("%d\n", (dargc -= 2, argc)) : 0x41627763),
main(argc+dargc, (char **) 0x77624163) : 0x41637762;
}
3. Find if the given number is a power of 2.

long testpow2(long n)
{
n = (n & 0xFFFF) + ((n & 0xFFFF0000) >> 16);
n = (n & 0xFF) + ((n & 0xFF00) >> 8);
n = (n & 0xF) + ((n & 0xF0) >> 4);
n = (n & 0x3) + ((n & 0xC) >> 2);
n = (n & 0x1) + ((n & 0x2) >> 1);
return !--n;
}
4. Multiply x by 7 without using multiplication (*) operator.

#define mult7(x) (((x) << 4) - ((x) << 3) - ((x) << 2) + ((x) << 1) + (x))
 
M

Martin Ambuhl

Jens said:
#define HEL "4$"
#define LO "c%"
#define WOR "s%2"
#define LD "$"
#define _ LD LO
#define HELLO HEL LO
#define WORLD "1"

void main(void)

The specification asked for a program in C. You've just fallen off the wagon.
 
D

Don Starr

[snips]
long testpow2(long n)
{
n = (n & 0xFFFF) + ((n & 0xFFFF0000) >> 16);
n = (n & 0xFF) + ((n & 0xFF00) >> 8);
n = (n & 0xF) + ((n & 0xF0) >> 4);
n = (n & 0x3) + ((n & 0xC) >> 2);
n = (n & 0x1) + ((n & 0x2) >> 1);
return !--n;
}

Does this work if a long is 64 bits and the bit set is one of the upper 32?

How about this:
int testpow2( long n )
{
return n ? 1 : 0;
}

I didn't see any restrictions in the original post (based on the quoted text above) stating that the
test was whether or not the "given number" was an integral, rational, or even real power of 2. (I
also didn't see any requirement that the test number be an integer - just using it here as an
example).
 
R

R. Rajesh Jeba Anbiah

I will be greatful if any one answers these questions?
Thank You.

These questions are frequently asked interview questions in
India. You would get flames if you post these "fun" questions (except
to Indians). You can get most of the answers by Googling
(www.google.com). Few answers are availabled at "A to Z of C" project
page ( http://guideme.itgo.com/atozofc/ )...
1. Write a "Hello World" program in 'C' without using a semicolon.

/* author: Joona I Palaste ??? */
#include <stdio.h>
int main ()
{
if ( printf ( "Hello World\n" ) > 0 )
{
}
}

2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;

//author: richard heathfield ???

#include <stdio.h>

#define TOP 100

int foo(int i)
{
printf("%d\n", i);
i < TOP && foo(i + 1);
printf("%d\n", i);
return i;
}

int main(void)
{
foo(1);
return 0;
}
3. Find if the given number is a power of 2.

Answer available in "A to Z of C"
4. Multiply x by 7 without using multiplication (*) operator.

//Logic: (n<<3)-n
//author: R. Rajesh Jeba Anbiah
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
for(i=0; i<10; ++i)
printf("%d*7 = %d\n", i, (i<<3)-i);
return 0;
}
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
6. Remove duplicates in array

//author: ??
int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
}
else
current++;
return insert;
}
7. Finding if there is any loop inside linked list.
8. Remove duplicates in an no key access database without using an
array
9. Convert (integer) number in binary without loops.

Answer available in "A to Z of C"
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.

Answer available in "A to Z of C"
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
13. Swap two numbers without using a third variable.

Answer available in "A to Z of C"

HTH,
R. Rajesh Jeba Anbiah
 
Joined
Jun 21, 2008
Messages
1
Reaction score
0
Malcolm said:
"Girish Pal Singh" <[email protected]> wrote in message
> I will be greatful if any one answers these questions?
>

I wonder what you tutor is looking for here. Maybe these are end of term
"fun" questions;
>
> 1. Write a "Hello World" program in 'C' without using a semicolon.
>

Hint - use an "if" statement.
>
> 2. Write a C++ program without using any loop (if, for, while etc) to
> print numbers from 1 to 100 and 100 to 1;
>

Brute force this is easy. Try clc++ for further hints.
>
> 3. Find if the given number is a power of 2.
>

You need to check that only one bit is set.
>
> 4. Multiply x by 7 without using multiplication (*) operator.
>

This is called backcracking
x * constant
can always be replaced by
(x << a) + (x << b) ...
>
> 5. Write a function in different ways that will return f(7) = 4 and
> f(4) = 7
>

presumably without using an if statement. You need to look at bitwise
operators.
>
> 6. Remove duplicates in array
>

This is actually a real task. It can be done by using nested loops.
>
> 7. Finding if there is any loop inside linked list.
>

You need to store the address of one of the nodes, then check if you revisit
it.
>
> 8. Remove duplicates in an no key access database without using an
> array
>

Got me here. "Never bullshit your way into a databse job".
>
> 9. Convert (integer) number in binary without loops.
>

Unroll the loop
>
> 10. Write a program whose printed output is an exact copy of the
> source. Needless to say, merely echoing the actual source file is
> not allowed.
>

look up "quine" on the web.
>
> 11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
> each player selects a number and adds it to the total. Once a
> number is used, it must be removed from the pool. The winner is the
> person whose number makes the total equal 31 exactly.
>

Your question?
>
> 13. Swap two numbers without using a third variable.
>

This is a stupid hack. I'll give you this one.

a ^= b; a ^= b; a ^= b.
another solution to this question: a=a+b; b=a-b;a=a-b;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top