Cc++ Tricky Programs
Several of these software applications are detailed here:
- An instruction for displaying text enclosed within double quotation marks (" ").
We employ quotation marks in the C++ programming language to mark the start and end of the text meant for output. This means that printing double quotes " requires the use of a specific escape sequence. To output quotes in C++, we will use the " symbol.
C++ Program:
// CPP code for printing double quotations
#include<iostream>
int main ()
{
std::cout << "\"javaLogic Practice\"";
return 0;
}
Output:
" javaLogic Practice "
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- To determine whether two numbers are equal without employing arithmetic or comparison operators.
The Bitwise XOR operator is the simplest approach for this task. Remember that XOR returns 0 when both values are the same. To address this issue, we will utilize the XOR operator.
C Program:
// C code to determine whether two values are equal instead of using arithmetic or comparison operators
#include<stdio.h>
int main ()
{
// Taking two variables
int a = 12;
int b = 12;
if ( !( a ^ b ) )
printf ( " a is equal to b" );
else
printf ( " a does not match b " );
return 0;
}
C++ Program:
// C++ code to determine whether two values are equal instead of using arithmetic or comparison operators
#include <iostream>
using namespace std;
int main ()
{
// Taking two variables
int a = 12;
int b = 12;
if ( !( a ^ b ) )
cout << " a is equal to b ";
else
cout << " a does not match b ";
return 0;
}
Output:
a is equal to b
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- Without using a semicolon, print all natural integers up to N. The concept of recursively calling the main function is used.
C++ Program:
// C++ code that prints all natural numbers up to N without the use of a semicolon.
#include<iostream>
using namespace std;
int N = 15;
int main ()
{
static int a = 9;
if ( cout << a << " " && a++ < N && main () )
{ }
return 0;
}
Output:
9 10 11 12 13 14 15
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- A Code to swap the values of two variables without the usage of a third variable.
C++ Program:
// C++ program to check if two numbers are equal
#include<bits/stdc++.h>
using namespace std;
int main ()
{
int a = 15;
int b = 30;
a = a + b;
b = a - b;
a = a - b;
cout << "A : " << a << "\n";
cout << "B : " << b << "\n";
return 0;
}
Output:
A : 30
B : 15
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- 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:
#include <stdlib.h>
// C++ code for obtaining the maximum and minimum of two values without the use of a loop or any condition.
#include<bits/stdc++.h>
int main ()
{
int p = 35, q = 26;
printf ( "maximum no = %d\n", ((p + q) + abs(p - q)) / 2);
printf ( "minimum no = %d", ((p + q) - abs(p - q)) / 2);
return 0;
}
Output:
maximum no = 35
minimum no = 26
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- In C, use the One's Complement Operator to output an unsigned int's maximum value.
Below is the procedure for utilizing the one's complement operator to determine the maximum value of an unsigned integer:
C Program:
// A C program that displays the largest value of an unsigned int.
#include<stdio.h>
int main ()
{
unsigned int maximum;
maximum = 0;
maximum = ~maximum;
printf ( "Maximum value : %u ", maximum );
return 0;
}
C++ Program:
// A C++ program that displays the largest value of an unsigned int.
#include <iostream>
int main ()
{
unsigned int maximum;
maximum = 0;
maximum = ~maximum;
std::cout << "Maximum value : " << maximum;
return 0;
}
Output:
Maximum value : 4294967295
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- 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:
// C++ code for displaying the sum of two numbers without the need for the + operator
#include<iostream>
using namespace std;
int main ()
{
int x = 15;
int y = 3;
int summation = -( -x - y );
cout << summation;
return 0;
}
Output:
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- Inside the if block, write code to check the condition.
C++ Program:
// CPP code that checks the condition inside the if block. It just validates the condition within the if block, i.e., cout << "Hello" returns a non-zero value,!(non-zero value) is false, so else is executed. As a result, theoretically, it merely performs the else block.
#include<iostream>
using namespace std;
int main ()
{
if ( !( cout << "Hello" ) )
cout << "Hello";
else
cout << " World";
return 0;
}
Output:
Hello World
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- 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:
// CPP code for dividing a number by 4 without using '/'
#include<iostream>
using namespace std;
int main ()
{
int num = 4;
num = num >> 2;
cout << num;
return 0;
}
Output:
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
- A code that verify the computer's endianness.
C Program:
// C code to determine whether the machine is little or big endian.
#include <stdio.h>
int main ()
{
unsigned int num = 1;
char *c = (char*) & num;
if (*c)
printf ( "THE MACHINE IS LITTLE ENDIAN") ;
else
printf ( "THE MACHINE IS BIG ENDIAN" );
return 0;
}
C++ Program:
// C++ code to determine whether the machine is little or big endian.
#include <iostream>
int main ()
{
unsigned int num = 1;
char *c = (char*) & num;
if (*c)
std::cout << "THE MACHINE IS LITTLE ENDIAN";
else
std::cout << "THE MACHINE IS BIG ENDIAN";
return 0;
}
Output:
THE MACHINE IS LITTLE ENDIAN
- Time Complexity will be O (1)
- Auxiliary Space will be O (1)
Conclusion:
C and C++ are widely-used programming languages employed for creating software applications, games, databases, and operating systems, among various other uses. Despite their similar names, C and C++ differ significantly in terms of functionality and use cases. C functions as a procedural programming language devoid of support for objects or classes, while C++ represents an evolved iteration of C that facilitates object-oriented programming. Let's delve into the disparities between these two programming languages.
Dennis Ritchie created it at Bell Laboratories in 1972. This platform-agnostic language was crafted for building utilities for the Unix operating system. It is currently extensively utilized across various domains.
It is an expansion of the traditional C language that integrates object-oriented programming capabilities and additional functionalities. C++ is a programming language closely related to lower-level languages and is acknowledged for its exceptional speed. C++ is employed for creating intricate, high-speed applications.