We may come across various tricky programs in our day-to-day life. Maybe in technical interviews, coding tests, or 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
#include <stdio.h>
int main()
{
printf("\"geeksforgeeks\"");
return 0;
}
C++
// CPP program to print double quotes
#include<iostream>
int main()
{
std::cout << "\"geeksforgeeks\"";
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
- To check if two numbers are equal without using arithmetic operators or comparison operators.
The simplest solution for this is using the Bitwise XOR operator (^). We know that for two equal numbers XOR operator returns 0. We will use the XOR operator to solve this problem.
C
// 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;
}
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
Time Complexity: O(1)
Auxiliary Space: O(1)
- Print all natural numbers up to N without using a semi-colon.
We use the idea of recursively calling the main function.
C
#include<stdio.h>
int N = 10;
int main()
{
static int x = 1;
if (printf("%d ", x) && x++ < N && main())
{ }
return 0;
}
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;
}
Output1 2 3 4 5 6 7 8 9 10
Time Complexity: O(1)
Auxiliary Space: O(1)
- To Swap the values of two variables without using any extra variable.
C
#include<stdio.h>
int main()
{
int x = 10;
int y = 70;
x = x + y;
y = x - y;
x = x - y;
printf("X : %d\n", x);
printf("Y : %d\n", y);
return 0;
}
C++
// C++ program to check if two numbers are equal
#include<bits/stdc++.h>
using namespace std;
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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
- Program to find the Maximum and minimum of two numbers without using any loop or condition.
The simplest trick is-
C
#include<stdio.h>
#include<stdlib.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;
}
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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
- 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;
}
C++
// C++ program to print maximum value of
// unsigned int.
#include <iostream>
int main()
{
unsigned int max;
max = 0;
max = ~max;
std::cout << "Max value : " << max;
return 0;
}
// This code is contributed by sarajadhav12052009
OutputMax value : 4294967295
Time Complexity: O(1)
Auxiliary Space: O(1)
- To find the 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
#include <stdio.h>
int main()
{
int a = 5;
int b = 5;
int sum = -( -a-b );
printf("%d",sum);
return 0;
}
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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
- Program to verify the condition inside if block.
C
#include <stdio.h>
int main()
{
if (!(printf("geeks")))
printf(" geeks ");
else
printf("forgeeks ");
return 0;
}
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;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
- Program to divide an integer by 4 without using '/' operator.
One of the most efficient ways 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;
}
C
#include <stdio.h>
int main()
{
int n = 4;
n = n >> 2;
printf(" %d ",n);
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(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;
}
C++
// C++ program to find if machine is little
// endian or big endian.
#include <iostream>
int main()
{
unsigned int n = 1;
char *c = (char*)&n;
if (*c)
std::cout << "LITTLE ENDIAN";
else
std::cout << "BIG ENDIAN";
return 0;
}
// This code is contributed by sarajadhav12052009
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
C++ Programming Basics C++ is a general-purpose programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented, and generic programming features. C++ runs on lots of platforms like Windows, Linux, Unix, Mac, etc.Before explaining the basics of C++, we would like to clarify
8 min read
C++ Tutorial | Learn C++ Programming C++ is a popular programming language that was developed as an extension of the C programming language to include OOPs programming paradigm. Since then, it has become foundation of many modern technologies like game engines, web browsers, operating systems, financial systems, etc.Features of C++Why
5 min read
10 C++ Programming Tricks That You Should Know C++ Programming Language is a powerful, versatile, and compiled language, which means the source code is converted into machine language before the execution. In order to make your code as efficient and effective as possible, you should be aware of the following tricks and techniques. Hence, it's be
10 min read
Output of C++ programs | Set 22 Predict the output of the following C++ programs. Question 1 CPP #include <iostream> using namespace std; int main() { int a = b = c = 0; cout << a << "*" << b << "*" << c; return 0; } Output: Compile time error! Explanation: A chained statemen
4 min read
Output of C++ programs | Set 42 Prerequisite : Pointers and References Q.1 What Is The Output Of this program? CPP #include <iostream> using namespace std; void fun(int& a, int b) { a += 2; b += 1; } int main() { int x = 10, y = 2; fun(x, y); cout << x << " " << y << " "; fun(x
4 min read
Output of C++ programs | Set 50 Predict the output of the following C++ programs:Question 1: CPP#include <cstdlib> #include <iostream> using namespace std; int main() { int ran = rand(); cout << ran << endl; return 0; } Output1804289383 Explanation: As the declared number is an integer, It will produce the
4 min read