# C puzzles...Lets c how many can u solve?? • ### Question

• Some companies certainly ask for these things. Specially Microsoft. Here are my favorite puzzles!!!

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. Swap two numbers without using a third variable.
14. Given an array (group) of numbers write all the possible sub groups of this group.

Wednesday, February 7, 2007 5:23 PM

### All replies

• 3.

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

cout<<"enter the values for a and b=";

cin>>a>>b;

cout<<"\nvalue of a and b before exchange="<<a<<b;

a=a+b;

b=a-b;

a=a-b;

cout<<"\nvalues of a and b after exchange="<<a<<b;

getch();

}

Friday, October 8, 2010 11:01 AM
• 5.

#include<iostream.h>

#include<conio.h>

void main();

{

int x,i,sum=0;

clrscr();

cout<<"enter the value of x=";

cin>>x;

for(i=1;i<=7;i++)

{

sum=sum+x;

}

cout<<"\nresult"<<sum;

getch();

}

Friday, October 8, 2010 11:07 AM
• 13.

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

cout<<"enter the values for a and b=";

cin>>a>>b;

cout<<"\nvalue of a and b before swapping="<<a<<b;

a=a+b;

b=a-b;

a=a-b;

cout<<"\nvalues of a and b after swapping="<<a<<b;

getch();

}

Friday, October 8, 2010 11:09 AM
• 4.

#include<iostream.h>

#include<conio.h>

void main()

{

int a,b=1;

clrscr();

cout<<"\n Enter the No. \n";

cin>>a;

while(b<a)

{  b*=2;}

if(b==a) cout<<"\n Number is a power of 2\n";

else cout<<"\n Number is not a power of 2\n";

getch();

}

Friday, March 18, 2011 12:19 PM
• 1. main()

{

if(printf("Hello World"))

{

}

return;

}

Friday, June 3, 2011 1:04 PM
• 5.

int main()

int a;
scanf( "%d", &a);
printf( "%d * 7 = %d", a, ((a << 3) - a) );
}

Thursday, July 7, 2011 11:37 AM
• 3.

int main()

int a;
int b;

scanf( "%d%d", &a, &b );

a ^= b;
b ^= a;
a ^= b;

printf( "a = %d, b = %d", a, b );
}

Thursday, July 7, 2011 11:40 AM
• 3 and 13 are same.

11. is a Quine. Here is one:

```#include <stdio.h>
char*f="#include <stdio.h> char*f=%c%s%c;void main(){printf(f,34,f,34,10);}%c";
void main(){
printf(f,34,f,34,10);
}
```

Regards,

Pankaj

Thursday, July 7, 2011 11:12 PM
• 2.

void main()

{

int a=0;

xy:

a++;

printf("%d ",a);

a<=100?goto xy:goto ab;

ab:

a--;

printf("%d ",a);

a>=0?goto ab:goto e;

e:

}

Wednesday, August 10, 2011 2:02 AM
• 4.

void main()

{

int n,n1;

printf("Enter a number");

scanf("%d",&n);

n1=n

while(n1!=0)

{

if(n1==1)

{

printf("%d is not power of 2",n);

break;

}

if(n1==0)

{

printf("%d is power of 2",n);

break;

}

n1=n1/2;

}

Wednesday, August 10, 2011 2:05 AM
• Remove duplicate elements from an ARRAY..

﻿void rmdup(int *array, int length)

{
int *current , *end = array + length - 1;

for ( current = array + 1; array < end; array++, current = array + 1 )
{
while ( current < end )
{
if ( *current == *array )
{
*current = *end--;
}
else
{
current++;
}
}
}
}

Wednesday, February 8, 2012 3:19 PM
• 4.

#include<stdio.h>

int main()
{
int num, i;

printf("Please enter a positive integer: ");
if(scanf("%d", &num)!=1) return (1);
for (i=0 ; i<=num/2 ; i++)
{
if(i*i==num)
{
puts("The number is a power of 2.");
break;
}
else if (i+1>num/2.0) puts("The number is NOT a power of 2.");
}
}

• Edited by Thursday, April 12, 2012 7:14 PM undesired embedded script tag by Lingoes
Thursday, April 12, 2012 9:20 AM
• #include<stdio.h>
#include<conio.h>
void main()
{

int a=10,b=20;
a=a+b;
b=a-b;
a=a-b;
printf("a=%d \t b=%d",a,b);
}
• Proposed as answer by Sunday, May 6, 2012 8:37 PM
Sunday, May 6, 2012 8:36 PM
• #include<stdio.h>
#include<conio.h>
void main()
{

int a=10,b=20;
a=a+b;
b=a-b;
a=a-b;
printf("a=%d \t b=%d",a,b);
}

it gives the correct output !!!

Sunday, May 6, 2012 8:37 PM
• raise to the  power                        in binary

2              0                       =1->                  1

2 1  =2->  10

2              2  =4->  100

we can check the binary forms using bitwise and shift operators

/* number in power of 2 */

#include<stdio.h>

main()

{

int num, b, power, flag;

printf("\n enter number");

scanf("%d", &num);

b=1;

power=0;

flag=0;

while(b<=num)

{

if( (num^b)==0)

{

flag++;

break;

}

power++;

b<<=1;

}

if(flag)

printf("%d is 2 raise to the power %d", num, power);

else

printf("%d is not any number raise to power 2",num);

return 0;

}

suppose i enter 2049 as input the above program takes 11 comparisons to figure out whether its a number is equal to any power of 2 which its not . similarly it take 2 comparisions in case

of 5. so to reduce number of comparisions we can can exploit a property that if a number is equal to any power raise to 2 it will have only one 1 which will be at the beginning of its binary. so here is another code

#include<stdio.h>
main()

int num,rem,flag=1, loops=0;

printf("enter a number");

scanf("%d",num);

while(num>1)
{
loops++;
rem=num%2;
num=num/2;

if(rem)
{
flag--;
break;
}
}
if(flag)
printf("yes is power of 2");
else
printf("it is not");
printf("\n loops=%d",loops);
return 0;
}

in above program number of loops also tell the power 2 is raised to get that number if it is.

both the programs do not work if the user enter number <=0

so we can also add a check .. i havent done that though..

Thursday, November 22, 2012 3:16 PM
• 10.

#include<stdio.h>

#include<conio.h>

#include<string.h>

int i;

char bin;

void intbin(int);

int main()
{
int a;
printf("enter the integer :\n");
scanf("%d",&a);
intbin(a);
getch();
return 0;
}
void intbin(int a){
int t,temp;
if(a!=0){
t=a%2;
if(t==0)
bin[i]='0';
else bin[i]='1';
i++;
intbin(a/2);
}
else{
strrev(bin);
puts(bin);
}
}

Friday, December 7, 2012 4:04 PM
• 4.

```void isPowOf2(unsigned int n) {
double val = log(n) / log(2);
if(floor(val) == val) printf("Its Power of 2\n");
else printf("Not\n");

return;
}```

Monday, August 4, 2014 12:19 PM