C/C++ Tricky Programs

Here are 10 tricky programs that will test your programming basics.
In C++ programming language, we use quotes to denote the start and end of the text is to be printed. So, printing quotes “ needs a special escape sequence. So, we will use the \” to print quotes in c++.
 Live Demo
#include<iostream>
using namespace std;
int main() {
   cout<<"\"Tutorials Point \"";
   return 0;
}
"Tutorials Point "
In programming to iterate over the same block of code multiple times, there are a few methods. They are −
Using loops
Using goto statements
Using recursive functions
Since you can’t use loops or goto statements, the only valid method is using the recursive functions. Let’s see how we can use the recursive calls to print numbers from 1 to 10.
 Live Demo
#include <stdio.h>
void printNumber(int count){
   printf("%d\n", count );
   count+=1;
   if(count<=10)
      printNumber(count);
}
int main(){
   printNumber(1);
   return 0;
}
1
2
3
4
5
6
7
8
9
10
To check for equality of two numbers we can use the bitwise XOR operator (^). If two numbers are equal then the bitwise XOR of these numbers is 0. Now, let’s implement this concept in a program.
 Live Demo
#include<iostream>
using namespace std;
int main(){
   int a = 132;
   int b = 132;
   if ( (a ^ b) )
      cout<<"a is not equal to b";
   else
      cout<<"a is else to b";
      return 0;
}
a is equal to b
In c/c++ programming language there are methods to print something without using a semicolon. We can do this by using the return type of the output method, printf. The printf method in c++ returns the number of characters printed to the output screen. We can use and the conditional statement that can execute without a semicolon.
 Live Demo
#include <stdio.h>
int main(){
   if(printf("Hello "))
   return 0;
}
Hello
To find the maximum and a minimum number of the two numbers that are defined without using a comparison operator we will make use of the abs method, and passing the difference of the two numbers to it. It will return the positive difference between the numbers and we will and subtract this absolute difference to find the max and min of the two given numbers.
 Live Demo
#include<iostream>
using namespace std;
int main (){
   int x = 15, y = 20;
   cout<<"The numbers are x = "<<x<<"and y = "<<y<<endl;
   cout<<"The max of the numbers is "<<((x + y) + abs(x - y)) / 2<<endl;
   cout<<"The min of the numbers is "<<((x + y) - abs(x - y)) / 2<<endl;
   return 0;
}
The numbers are x = 15and y = 20
The max of the numbers is 20
The min of the numbers is 15
To print the source code of the program as the output of the same program is a bit of a tricky question and needs quite a good understanding of the programming language to do.
In this program, we will use the concepts of file handling and open the same file which we are using to write our code and then print the content of the file.
#include <stdio.h>
int main(void){
   FILE *program;
   char ch;
   program = fopen(__FILE__, "r");
   do{
      ch=fgetc(program);
      printf("%c", ch);
   }
   while(ch!=EOF);
      fclose(program);
   return 0;
}
A sum of two numbers can be found without using the + operator by using the - operator multiple times in the code. The below program shows how to.
 Live Demo
#include<iostream>
using namespace std;
int main(){
   int x = 5;
   int y = 5;
   int sum = x - (-y);
   cout<<"The numbers are x = "<<x<<" y = "<<y<<endl;
   cout<<"Their sum = "<<sum;
   return 0;
}
The numbers are x = 5 y = 5
Their sum = 10
To check if the given number is even or not, we can use bitwise operators. The bitwise & operator along with 0x01 will check for the bit at the 0th position in the number. If the bit at 0th position is 1 then the number is odd otherwise it is even.
 Live Demo
#include<iostream>
using namespace std;
int main(){
   int a = 154;
   if(a & 0x01) {
      cout<<a<<" is an odd number";
   } else{
      cout<<a<<" is an even number";
   }
   printf("\n");
   return 0;
}
154 is an even number
To divide a number by 4 without using a divide operator, we can use the right shift operator >> that shifts the last bit.
 Live Demo
#include<iostream>
using namespace std;
int main(){
   int n = 128;
   cout<<n<<"divided by 4 = ";
   n = n >> 2;
   cout<< n;
   return 0;
}
128 divided by 4 = 32
Calculating recursive sum by adding all digits of the number and then see if it is single digit then stop otherwise recalculate the sum until the sum becomes single digit.
 Live Demo
#include <iostream>
using namespace std;
int main() {
   int a = 534;
   int sum;
   if(a)
      sum = a % 9 == 0 ? 9 : a % 9 ;
   else
      sum = 0;
   cout<<"The final sum is "<<sum;
   return 0;
}
The final sum is 3