C/C++ Tricky Programs

In this tutorial, we will explore some amazing tricky programs in C/C++ programming language.

In our daily lives, we may come across a variety of perplexing programs, perhaps during technical interviews, coding assessments, or C/C++ classes.

A number of these programmes are listed below:

1. A code for printing text that should be surrounded by double quote marks (" ").

We utilise quotes in the C++ programming language to indicate the beginning and end of the text to be printed. As a result, printing quotations " necessitates the use of a particular escape sequence. To print quotations in C++, we shall utilise the " symbol.

C++ Program:

Output:

" javaTpoint "
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

2. To determine whether two numbers are equal without employing arithmetic or comparison operators.

The Bitwise XOR operator () is the easiest solution for this. Keep in mind that the XOR operator gives 0 for two equal values. To deal with this problem, we shall employ the XOR operator.

C Program:

C++ Program:

Output:

a is equal to b
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

3. Without using a semicolon, print all natural integers up to N. The concept of recursively calling the main function is used.

C++ Program:

Output:

9 10 11 12 13 14 15
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

4. A Code to swap the values of two variables without the usage of a third variable.

C++ Program:

Output:

A : 30
B : 15
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

5. Without using a loop or condition, write a program to identify the maximum and minimum of two values.

The most basic trick is-

C++ Program:

Output:

maximum no = 35
minimum no = 26
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

6. In C, use the One's Complement () Operator to output an unsigned int's maximum value.

Below is how we use the one's complement operator to find an unsigned int's maximum value:

C Program:

C++ Program:

Output:

Maximum value : 4294967295
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

7. To compute the sum of two numbers without relying on the '+' operator. This is a simple mathematical approach. We already understand that a + b = - (-a-b). So this will serve as a ruse for us.

C++ Program:

Output:

18
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

8. Inside the if block, write code to check the condition.

C++ Program:

Output:

Hello World
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

9. Code to divide an integer by 4 without using the '/' operator. The right shift operator (">>") is one of the effective methods for dividing an integer by 4.

C++ Program:

Output:

1
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

10. A code that verify the computer's endianness.

C Program:

C++ Program:

Output:

THE MACHINE IS LITTLE ENDIAN
  • Time Complexity will be O (1)
  • Auxiliary Space will be O (1)

Conclusion:

C and C++ are programming languages used to develop programmes, games, database systems, and operating systems, among other things. While the names C and C++ are similar, their features and applications are not. C is a procedural programming language that does not support objects or classes. C++ is an enhanced version of C that supports object-oriented programming. Let's look at the distinctions between the C and C++ programming languages.

Dennis Ritchie invented it at Bell Laboratories in 1972. This machine-independent language was designed to be used in the development of tools for the Unix platform. It is now widely employed in a range of applications.

SIt is an extension of the standard C language that incorporates support for object-oriented programming and other features. C++ is a computer language that is close to low-level languages and is recognised as one of the fastest. C++ is a programming language used to develop complex, high-performance programmes.