C/C++ Tricky Programs - GeeksforGeeks

28 Mar, 2022
We may come across various tricky programs in our day to day life. Maybe in technical interviews, coding tests, or in C/C++ classrooms. 
Here is a list of such programs:- 
Print text within double quotes (” “). This may seem easy but beginners may get puzzled while printing text within double-quotes.
C++
// CPP program to print double quotes#include<iostream> int main(){   std::cout << "\"geeksforgeeks\"";   return 0;}
Output: 
"geeksforgeeks"
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++
C
// C++ program to check if two numbers are equal// without using arithmetic operators or// comparison operators#include <iostream>using namespace std; int main(){   int x = 10;   int y = 10;       if (!(x ^ y))      cout << " x is equal to y ";   else      cout << " x is not equal to y ";          return 0;} // This code is contributed by shivani
// 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
Print all natural numbers upto N without using semi-colon. We use the idea of recursively calling main function.
C++
// C++ 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 
To Swap the values of two variables without using any extra variable.
C++
// C++ program to check if two numbers are equal#include<iostream> int main(){   int x = 10;   int y = 70;    x = x + y;   y = x - y;   x = x - y;    cout << "X : " << x << "\n";   cout << "Y : " << y << "\n";       return 0;}
Output: 
X : 70
Y : 10
Program to find Maximum and minimum of two numbers without using any loop or condition. The simplest trick is-
C++
// C++ 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
Print the maximum value of an unsigned int using One’s Complement (~) Operator in C. Here is a trick to find the maximum value of an unsigned int using one’s complement operator:
C
// 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;}   
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.
C++
// CPP program to print sum of two integers// without +#include<iostream> using namespace std;int main(){  int a = 5;  int b = 5;  int sum = -( -a-b );  cout << sum;  return 0;}
Output: 
10
Program to verifies the condition inside if block.
C++
// 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 << "geeks"))    cout <<" geeks ";    else    cout << "forgeeks ";         return 0;}
Output: 
geeksforgeeks
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 (“>>”).
C++
// 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
Program to check endianness of the computer.
C
// 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;}
This article is contributed by Smitha Dinesh Semwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Rajiv Saxena
harinibachu
tusharmonga014
shivanisinghss2110
sooda367
sumitgumber28
c-puzzle
cpp-puzzle
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
TCP Server-Client implementation in C
Exception Handling in C++
Multithreading in C
'this' pointer in C++
UDP Server-Client implementation in C
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects