There are various tricky programs in our day to day life. May be in technical interviews, coding tests, or in C/C++ classrooms.
Here are some of such programs:
1. Print text within double quotes (” “).
This may seem easy but beginners may get puzzled while printing text within double quotes.
// CPP program to print double quotes #include<iostream> int main() { std::cout << "\"geeksforgeeks\""; return 0; }
Output:
"geeksforgeeks"
2. To check if two numbers are equal without using arithmetic operators or comparison operators.
The simplest solution for this is using Bitwise XOR operator (^). We know that, for two equal numbers XOR operator returns 0. We will use this trick to solve this problem.
// C program to check if two numbers are equal // without using arithmetic operators or // comparison operators #include<stdio.h> int main() { int x = 10; int y = 10; if ( !(x ^ y) ) printf(" x is equal to y "); else printf(" x is not equal to y "); return 0; }
Output:
x is equal to y
3. Print all natural numbers upto N without using semi-colon. We use the idea of recursively calling main function.
// CPP program to print all natural numbers upto // N without using semi-colon #include<iostream> using namespace std; int N = 10; int main() { static int x = 1; if (cout << x << " " && x++ < N && main()) { } return 0; }
Output:
1 2 3 4 5 6 7 8 9 10
4. Program to find Maximum and minimum of two numbers without using any loop or condition.
The simplest trick is-
// CPP program to find maximum and minimum of // two numbers without using loop and any // condition. #include<bits/stdc++.h> int main () { int a = 15, b = 20; printf("max = %d\n", ((a + b) + abs(a - b)) / 2); printf("min = %d", ((a + b) - abs(a - b)) / 2); return 0; }
Output:
max = 20 min = 15
5. Print the maximum value of an unsigned int using One’s Compliment (~) Operator in C.
Here is a trick to find maximum value of an unsigned int using one’s compliment operator:
// C program to print maximum value of // unsigned int. #include<stdio.h> int main() { unsigned int max; max = 0; max = ~max; printf("Max value : %u ", max); return 0; }
Output:
Max value : 4294967295
6. To find sum of two integers without using ‘+’ operator.
This is a very easy mathematics trick.
We know that a + b = – (-a-b). So this will work as a trick for us.
// CPP program to print sum of two integers // withtout + #include<iostream> using namespace std; int main() { int a = 5; int b = 5; int sum = -( -a-b ); cout << sum; return 0; }
Output:
10
7. Program to verifies the condition inside if block.
// CPP program to verifies the condition inside if block // It just verifies the condition inside if block, // i.e., cout << "geeks" which returns a non-zero value, // !(non-zero value) is false, hence it executes else // Hence technically it only executes else block #include<iostream> using namespace std; int main() { if (!(cout << "hello")) cout <<" hello"; else cout << " world"; return 0; }
Output:
hello world
8. Program to divide an integer by 4 without using ‘/’ operator.
One of the most efficient way to divide an integer by 4 is to use right shift operator (“>>”).
// CPP program to divide a number by 4 // without using '/' #include<iostream> using namespace std; int main() { int n = 4; n = n >> 2; cout << n; return 0; }
Output:
1
9. Program to check endianness of the computer.
// C program to find if machine is little // endian or big endian. #include <stdio.h> int main() { unsigned int n = 1; char *c = (char*)&n; if (*c) printf("LITTLE ENDIAN"); else printf("BIG ENDIAN"); return 0; }
Output:
LITTLE ENDIAN
10. Write a c++ program to print fibonacci series without using recursion.
#include <iostream> using namespace std; int main() { int n1=0,n2=1,n3,i,number; cout<<"Enter the number of elements: "; cin>>number; cout<<n1<<" "<<n2<<" "; //printing 0 and 1 for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; cout<<n3<<" "; n1=n2; n2=n3; } return 0; }
Output:
Enter the number of elements: 10 0 1 1 2 3 5 8 13 21 34