Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Brain Teasers in c programming

Expressions

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { int ans=2; int m=10; int k; k=!((ans<2)&&(m>2)); printf("\n%d",k); getch(); }

Output
1

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { int m,j=3,k; m=2*j/2; k=2*(j/2); printf("\nm=%d k=%d",m,k); getch(); }

Output
Ans: m=3 k=2

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { int x,y,z; x=2;y=2; x=2*(y++); z=2*(++y); printf("\nx=%d y=%d z=%d",x,y,z); getch(); }

X=4 y=4 z=8

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { int x=!0*10; printf("\nx=%d",x); getch(); }

Output
Ans: x=10

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { printf("\n%d",(10/3)*3+5%3); getch(); }

Output
Ans = 11

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { printf("\n%d",(23%2)%(int)5.5); getch(); }

Output
1

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { printf("\n%d",16>>2); getch(); }

Output
Ans:4

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { printf("\n%d",5&&2); printf("\n%d,0&&2); getch(); }

Output
If both operands are nonzero it will return 1 Ans:1 Ans: 0

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { int a,b,c; a=9; b=10; c= (b<a || b>a ); printf("\n c=%d",c); getch(); }

Output
Ans : c =1

Brain Teasers
#include<stdio.h> #include<conio.h> void main() { int b,k=8; b= (k++-k++-k++,k++); printf("\n b=%d",b); printf(\n k=%d,k); getch(); }

Output
Ans : b=11 k=12 In comma operator the expressions are evaluated from left to right and value of the right most expressions takes the evaluated value(k=11) of the left

Which of the following is incorrect? a. n=m=0 b) value+=10 c) a = x<y ? 9: 11 d) b= (x>5 || x<0) e) none

Output
Ans: e

Brain Teasers
void main() { float x=3.14; printf("\n %f",x%2); getch(); }

Ans: error Since modulo operator can be applied only for integers.

Brain Teasers
void main() { int x=-11; printf("\n %d",x%2); getch(); }

Output
Ans: -1 Sign of the result is always the sign of the first operand.

Brain Teasers
void main() { printf("\n %d",'A'); getch(); }

Output
Ans: 65 Display the ascii value of A

Brain Teasers
void main() { double d=1/2.0-1/2; printf("\n d= %.2lf",d); getch(); }

Output
Ans: d= 0.50 If any one of the operand is real then the result is always a real number

Brain Teasers
void main() { unsigned int c=-2; printf("\n c= %u",c); getch(); }

Output
Ans : garbage value Unsigned int only accepts positive numbers.

Brain Teasers
void main() { printf("\n %c",'A' + 10); getch(); }

Output
Ans : K

Brain Teasers
void main() { int a =5; a=printf("Good")+printf("Morning"); printf("\n %d",a); getch(); }

Output
Ans: Good Morning 11

Brain Teasers
void main() { int a =5; a=printf("%d%d",a,a); printf("\n %d ",a); getch(); }

Output
Ans : 5 5 2

Brain Teasers
void main() { printf("cse" "department"); getch(); }

Ans : csedepartment

Brain Teasers
void main() { int c = - -2; printf("%d",c); getch(); }

Output
Ans : 2

Brain Teasers
void main() { int c = --2; printf("%d",c); getch(); }

Output
Error Increment(++) and Decrement (--) operators cannot be applied to constant.

Brain Teasers
void main() { int a=5; int i=!a > 10; printf("%d",i); getch(); }

Output
0

Brain Teasers
void main() { int a=5; printf("%d %d %d %d %d %d %d",a++,a,++a); getch(); }

6 6 6 garbage values

void main() { int i,j; printf("%d",scanf("%d%d",&i,&j)); getch(); }

Output
234 567 2

Brain Teasers
void main() { int x=2,y=5; printf("%d",++(x+y)); getch(); }

Output
Error

Brain Teasers
void main() { int x=1,y=5; printf("%d",++x+y); getch(); }

Output
7

Brain Teasers
void main() { int x=1; x=x+2*x++; printf("%d",x); getch(); }

Output
4

You might also like