Quote:
Originally Posted by
ralu.
Q1 Write a "Hello World" program in 'C' without using a semicolon.
Note that C99 doesn't require "return" explicitly:
Code:
#include <stdio.h>
int main()
{
if(printf("Hello World!\n")) { }
}
Quote:
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Code:
cout << "1 2 3 4 ... 99 100" << endl;
cout << "100 99 ... 4 3 2 1" << endl;
Quote:
Q3 C/C++ : Exchange two numbers without using a temporary variable.
Note that you may not abbreviate the following:
Code:
int a, b;
a ^= b;
b ^= a;
a ^= b;
Quote:
Q4 C/C++ : Find if the given number is a power of 2.
Bit twiddling or simply the following:
Code:
log2(x) == floor(log2(x))
Quote:
Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
Code:
x = x+x+x+x+x+x+x;
/* or */
x = (x<<3) - x;
Quote:
Q6 C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7
Code:
/* the obvious approach or this one: */
int f(int x)
{
return x ^ 3;
}
Code:
Q7 Remove duplicates in array
Where's the puzzle?
Quote:
Q8 Finding if there is any loop inside linked list.
Haire-and-Tortoise approach / Classic Floyd's Cycle Finding Algorithm: Two pointers "slow" and "fast" pointing to the first element. "slow" advances one element per iteration, "fast" advances two. If slow == fast before the end is reached, the list contains a cycle.
Quote:
Q9 Remove duplicates in an no key access database without using an array
See Q7.
Quote:
Q10 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.
Or better: write a program in C that prints a program in Perl that prints the original C program. Once done, this is easily extended to an arbitrary number of steps.
Quote:
Q11 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.
Too much overhead. Any volunteers? ;)
Quote:
Q12 Swap two numbers without using a third variable.
Solved in Q3.
Quote:
Given an array (group) of numbers write all the possible sub groups of this group.
Where's the puzzle?
Quote:
Q14 Convert (integer) number in binary without loops.
Greets,
Philip