C puzzles...Lets c how many can u solve??

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

• mercoledì 7 febbraio 2007 17:23

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.

### Tutte le risposte

• venerdì 8 ottobre 2010 11:01

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();

}

• venerdì 8 ottobre 2010 11:07

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();

}

• venerdì 8 ottobre 2010 11:09

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();

}

• venerdì 18 marzo 2011 12:19

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();

}

• venerdì 3 giugno 2011 13:04

1. main()

{

if(printf("Hello World"))

{

}

return;

}

• giovedì 7 luglio 2011 11:37

5.

int main()

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

• giovedì 7 luglio 2011 11:40

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 );
}

• giovedì 7 luglio 2011 23:12

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

• mercoledì 10 agosto 2011 02:02

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:

}

• mercoledì 10 agosto 2011 02:05

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;

}

• mercoledì 8 febbraio 2012 15:19

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++;
}
}
}
}

• giovedì 12 aprile 2012 09:20

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.");
}
}

• Modificato giovedì 12 aprile 2012 19:14 undesired embedded script tag by Lingoes
•
• domenica 6 maggio 2012 20:36

#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);
}
• Proposto come risposta domenica 6 maggio 2012 20:37
•
• domenica 6 maggio 2012 20:37

#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 !!!

• giovedì 22 novembre 2012 15:16

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..

• venerdì 7 dicembre 2012 16:04

10.

#include<stdio.h>

#include<conio.h>

#include<string.h>

int i;

char bin[50];

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[20];
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);
}
}