C/C++ Tricky Programs
We may come across various tricky programs in our day to day life. May be 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.// CPP program to print double quotes#include<iostream>intmain(){std::cout <<"\"geeksforgeeks\"";return0;}chevron_rightfilter_noneOutput:
"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 program to check if two numbers are equal// without using arithmetic operators or// comparison operators#include<stdio.h>intmain(){intx = 10;inty = 10;if( !(x ^ y) )printf(" x is equal to y ");elseprintf(" x is not equal to y ");return0;}chevron_rightfilter_noneOutput:
x is equal to y
- 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>usingnamespacestd;intN = 10;intmain(){staticintx = 1;if(cout << x <<" "&& x++ < N && main()){ }return0;}chevron_rightfilter_noneOutput:
1 2 3 4 5 6 7 8 9 10
- To Swap the values of two variables without using any extra variable.
// C++ program to check if two numbers are equal#include<iostream>intmain(){intx = 10;inty = 70;x = x + y;y = x - y;x = x - y;cout <<"X : "<< x <<"\n";cout <<"Y : "<< y <<"\n";return0;}chevron_rightfilter_noneOutput:
X : 70 Y : 10
- 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>intmain (){inta = 15, b = 20;printf("max = %d\n", ((a + b) +abs(a - b)) / 2);printf("min = %d", ((a + b) -abs(a - b)) / 2);return0;}chevron_rightfilter_noneOutput:
max = 20 min = 15
- 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>intmain(){unsignedintmax;max = 0;max = ~max;printf("Max value : %u ", max);return0;}chevron_rightfilter_none - 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>usingnamespacestd;intmain(){inta = 5;intb = 5;intsum = -( -a-b );cout << sum;return0;}chevron_rightfilter_noneOutput:
10
- 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>usingnamespacestd;intmain(){if(!(cout <<"geeks"))cout <<" geeks ";elsecout <<"forgeeks ";return0;}chevron_rightfilter_noneOutput:
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 (“>>”).// CPP program to divide a number by 4// without using '/'#include<iostream>usingnamespacestd;intmain(){intn = 4;n = n >> 2;cout << n;return0;}chevron_rightfilter_noneOutput:
1
- Program to check endianness of the computer.
// C program to find if machine is little// endian or big endian.#include <stdio.h>intmain(){unsignedintn = 1;char*c = (char*)&n;if(*c)printf("LITTLE ENDIAN");elseprintf("BIG ENDIAN");return0;}chevron_rightfilter_none
This article is contributed by Smitha Dinesh Semwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.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.
Recommended Posts:
- Output of C++ programs | Set 36
- Output of C programs | Set 63
- Output of C++ programs | Set 37
- Output of C++ programs | Set 22
- Output of C++ programs | Set 25 (Macros)
- Output of C++ programs | Set 29 (Strings)
- Memory Layout of C Programs
- Error Handling in C programs
- Output of C programs | Set 30 (Switch Case)
- Programs to print Interesting Patterns
- How to Compile and Run C/C++/Java Programs in Linux
- Programs for printing pyramid patterns in C++
- Common Memory/Pointer Related bug in C Programs
- Facts and Question related to Style of writing programs in C/C++

