C/C++ Tricky Programs

CC++Server Side ProgrammingProgramming

Here are 10 tricky programs that will test your programming basics.

1. Program to print “ ” in C++

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++.

Example

 Live Demo

#include<iostream>
using namespace std;
int main() {
   cout<<"\"Tutorials Point \"";
   return 0;
}

Output

"Tutorials Point "

2. Program to print numbers from 1 to 10 with using loops or goto statements

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.

Example

 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;
}

Output

1
2
3
4
5
6
7
8
9
10

3. To check for equality of two numbers without using arithmetic or comparison operator

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.

Example

 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;
}

Output

a is equal to b

4. Print “Hello” without using a; in c/c++

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.

Example

 Live Demo

#include <stdio.h>
int main(){
   if(printf("Hello "))
   return 0;
}

Output

Hello

5. Program to find the maximum and minimum of the two numbers without using the comparison operator.

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.

Example

 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;
}

Output

The numbers are x = 15and y = 20
The max of the numbers is 20
The min of the numbers is 15

6.Print the source code of program and output

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.

Example

#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;
}

7. Program to find the sum of two numbers without using + operator

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.

Example

 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;
}

Output

The numbers are x = 5 y = 5
Their sum = 10

8. To check if the given number is even without using arithmetic or relational operators.

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.

Example

 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;
}

Output

154 is an even number

9. Program to divide a number by 4 without using / operator.

To divide a number by 4 without using a divide operator, we can use the right shift operator >> that shifts the last bit.

Example

 Live Demo

#include<iostream>
using namespace std;
int main(){
   int n = 128;
   cout<<n<<"divided by 4 = ";
   n = n >> 2;
   cout<< n;
   return 0;
}

Output

128 divided by 4 = 32

10.C++ program to recursively calculate the sum of digits of a number until it becomes a single-digit number.

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.

Example

 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;
}

Output

The final sum is 3
raja
Published on 19-Sep-2019 11:37:22
Advertisements