C/C++ Puzzles
Posted by Subash  8:09 PM
[23] Comments

links to this post
Some companies certainly ask for these puzzles. Specially Microsoft.
1. Write 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;
3. C/C++ : Exchange two numbers without using a temporary variable.
4. C/C++ : Find if the given number is a power of 2.
5. C/C++ : Multiply x by 7 without using multiplication (*) operator.
6. C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7
7. Remove duplicates in array
8. Finding if there is any loop inside linked list.
9. Remove duplicates in an no key access database without using an array
10. Convert (integer) number in binary without loops.
11. 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.
12. 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. Given an array (group) of numbers write all the possible sub groups of this group.
2:36 AM
where are the ans.......
we also looking for ans and not only the questions :(
1:10 PM
can't speak for every question but the ones which probhits use of loop can be solved using recursive function and the variable swap using pointers.
i'm too searching for rest of the answers so smone please oblige.
8:52 AM
I dont think you can solve the variable swap using pointers?
You can use xor
say you have two variables a, b
the key fact to remember is that
xor is reversible and commutable (or you can think of it being like a toggle)
x xor y xor x=y
x xor y xor y=x
so have
b=a ^ b;
a=b ^ a; (i.e a xor b xor a=b)
b=b ^ a (i.e a xor b xor b=a)
^ is c operator for xor
4:15 AM
To swap variables without using a temporary variables, you can use the following method (we want to change a in b and vice et versa):
a=a+b
b=a
a=ab
The content is exchanged...
See you
7:06 PM
Your swapping algorithm has error
anfortunatly.
The right answer is:
void Swap(int a,int b)
{
a = a+b;
b = ab;
a = ab;
}
8:41 PM
To swap values of two variables A and B:
A = A + B
B = A  B
A = A  B
For example, if A=2 and B=5, then following above steps:
A = 2 + 5 = 7
B = 7  5 = 2
A = 7  2 = 5
3:36 PM
hmmm...here is d pgm to print "hello world" witout usin ';'
main()
{
if(printf("hello world"))
{
}
}
ther's yet another kool ques i hav.chk whether given no is odd r even witout usin conditional n loopin stts....
here's d ans..
main()
{
int a;
char n[][]={"even","odd"};
printf("enter d no:");
scanf("%d",&a);
printf("The no is %s",n[a%2]);
}
howz tat:))))
3:37 PM
hmmm...here is d pgm to print "hello world" witout usin ';'
main()
{
if(printf("hello world"))
{
}
}
ther's yet another kool ques i hav.chk whether given no is odd r even witout usin conditional n loopin stts....
here's d ans..
main()
{
int a;
char n[][]={"even","odd"};
printf("enter d no:");
scanf("%d",&a);
printf("The no is %s",n[a%2]);
}
howz tat:))))
3:06 PM
here's program in C++ to print 1..100 without using loops and ifs ..
class Fundoo
{
static int i;
public:
Fundoo()
{
cout«i++«endl;
}
};
int Fundoo::i=1;
int main()
{
Fundoo arr[100];
}
Thats it ;) !!!
5:49 PM
To Find if the given number is a power of 2.
int number;
int compareBit = 1;
cin>>number;
if( number&compareBit )
{
cout<<"Number is not power of 2";
}
else
{
cout<<"number is power of 2";
}
9:18 AM
10)Convert (integer) number in binary without loops.
its answer can be done using recursion
void dbconvert(int x)
{
if(x==0)
return;
else
dbconvert(x/2);
printf("%d",x%2);
}
8:32 PM
Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;
Answer ::
Using environment variables we can print these values
"setjump, longjump"
5:09 PM
Number multiplication by 7 without using the *
int nNumber = 10;
nNumber = (nNumber << 3)  nNumber
6:45 AM
I am a software engineer, and sometimes I feel that puzzle questions like these can be condescending, ambiguously worded, and fairly subjective as to how one's answer/solution is rated. For instance, sometimes I wonder how creative they want the solution to be. For instance, for the question about multiplying a number by 7 without using the * operator, one could do this:
int aNumber = 5;
int originalValue = aNumber;
for (int i = 0; i < 6; ++i)
aNumber += originalValue;
Based on what the question asks for, that seems like a perfectly valid solution to me, but in the back of my mind, I'd wonder that they would be looking for a really clever and a possibly obscure solution.
2:59 AM
2. Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1
using std::cout;
using std::endl;
int COUNTER = 0;
void printOneTo100()
{
cout << COUNTER << endl;
if (COUNTER == 100)
return;
++COUNTER;
printOneTo100();
}
void print100ToOne()
{
cout << COUNTER << endl;
if (COUNTER == 1)
return;
COUNTER;
print100ToOne();
}
int main(int argc, char* argv[])
{
COUNTER = 1;
printOneTo100();
print100ToOne();
return(0);
}
6:05 AM
The multiply by seven one is a bit easy.
y=x/(1/7)
12:46 AM
If you do y=x/(1/7), I imagine you'd have to be careful if you are using integers, or else the result would probably be 0. To really be careful, you'd probably have to cast x to double, then cast the result back to int, as in:
int y = (int)((double)x/(1.0/7.0))
11:40 AM
int x = 101;
void hello()
{
exit(1);
}
void bye()
{
x=x1;
printf("number = %d\n",x);
}
int main()
{
x==1?hello():bye();
main();
return 0;
}
This is in c, can be done in c++ also. 1 of solution given here uses if() which is not to be used, one function call can be avoided calling exit directly
6:18 PM
you can also use goto statement for printing 1 t0 100
as
void main()
{
int a=1;
label:
cout<< a;
a = a + 1;
if ( a < 100)
goto label;
}
5:22 PM
Hi,
To find if a number is power of 2 or not, we can use the following approach :
if (x & (x1))
printf("power of 2");
else
printf("Not a power of 2");
1:45 AM
The challenges are pretty fine here
also try these challenges
http://www.prog2impress.com/puzzles.html
Thanks,
Jaam
http://www.prog2impress.com/puzzles.html
5:45 PM
//Solution for puzzle 2
#include "stdio.h"
#include "conio.h"
void print(int);
int main(){
print(1);
getch();
}
void print(int i) {
if(i == 101)
return;
else {
printf("%d\n", i);
print(++i);
printf("%d\n", i1);
}
}
2:05 AM
/*Solution to puzzle 2 using recursion*/
#include
void recurssion(int x,int f)
{
cout<<x;
if(x==100)
f=0;
if(f==1)
x++;
else if(x==0)
x;
if(x==0)
return;
recurssion(x,f);
}
void main()
{
recurssion(1,1);
}