You are on page 1of 9

10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

Custom Search

COURSES Login

HIRE WITH US


C/C++ Tricky Programs
We may come across various tricky programs in our day to day life. May be in
technical interviews, coding tests, or in C/C++ classrooms.
Here is a list of such programs :-

1. Print text within double quotes (” “).


This may seem easy but beginners may get puzzled while printing text within
double quotes.

// CPP program to print double quotes


#include<iostream>

int main()
{
std::cout << "\"geeksforgeeks\"";
return 0;
}

Output:

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 1/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

"geeksforgeeks"

2. To check if two numbers are equal without using arithmetic operators or


comparison operators.
The simplest solution for this is using Bitwise XOR operator (^). We know that, for
two equal numbers XOR operator returns 0. We will use this trick to solve this
problem.

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

Output:

x is equal to y

3. Print all natural numbers upto N without using semi-colon.


We use the idea of recursively calling main function.

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 2/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

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

Output:

1 2 3 4 5 6 7 8 9 10

4. Program to nd Maximum and minimum of two numbers without using any loop
or condition.
The simplest trick is-

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

Output:

max = 20
min = 15

5. Print the maximum value of an unsigned int using One’s Compliment (~)
Operator in C.
Here is a trick to nd maximum value of an unsigned int using one’s compliment
operator:

// C program to print maximum value of


// unsigned int.
#include<stdio.h>

int main()
https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 3/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

{
unsigned int max;
max = 0;
max = ~max;
printf("Max value : %u ", max);
return 0;
}

6. To nd 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.

// CPP program to print sum of two integers


// withtout +
#include<iostream>

using namespace std;


int main()
{
int a = 5;
int b = 5;
int sum = -( -a-b );
cout << sum;
return 0;
}

Output:

10

7. Program to veri es the condition inside if block.

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

Output:

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 4/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

geeksforgeeks

8. Program to divide an integer by 4 without using ‘/’ operator.


One of the most e cient way to divide an integer by 4 is to use right shift
operator (“>>”).

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

Output:

9. Program to check endianness of the computer.

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

This article is contributed by Smitha Dinesh Semwal. If you like GeeksforGeeks and
would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org.
See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you nd anything incorrect, or you want to share more
information about the topic discussed above.

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 5/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

Recommended Posts:
Output of C++ programs | Set 36
Output of C++ programs | Set 37
Output of C programs | Set 63
Output of C++ programs | Set 22
Error Handling in C programs
Output of C++ programs | Set 29 (Strings)
Memory Layout of C Programs
Output of C++ programs | Set 25 (Macros)
How to Compile and Run C/C++/Java Programs in Linux
Programs to print Interesting Patterns
Programs for printing pyramid patterns in C++
Output of C programs | Set 30 (Switch Case)
Common Memory/Pointer Related bug in C Programs
Facts and Question related to Style of writing programs in C/C++

Improved By : Rajiv Saxena, harinibachu

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 6/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

Article Tags : C C++ c-puzzle cpp-puzzle

Practice Tags : C CPP


11

2.9
To-do Done
Based on 14 vote(s)

Feedback/ Suggest Improvement Add Notes Improve Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 7/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

Comments Community 
1 Login

 Recommend t Tweet f Share Sort by Newest

Join the discussion…

LOG IN WITH
OR SIGN UP WITH DISQUS ?

Name

Anonyumos • a year ago


It's Easy And Learnable Post

I am Very Thankful to GeeksforGeeks.

Thanks
△ ▽ • Reply • Share ›

Aditya Pai • a year ago


>> this is right shift not left shift
if you use left shift you're multiplying it by 4
1△ ▽ • Reply • Share ›

LEARN BY LIFE : BY EXPERIENCE • 2 years ago


In question 8 there is right shift operator not left.
thanks...
△ ▽ • Reply • Share ›

fady • 2 years ago


can't understand number 9
△ ▽ • Reply • Share ›

Smita Semwal > fady • 2 years ago


refer
http://www.geeksforgeeks.or...
△ ▽ • Reply • Share ›

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 8/9
10/17/2019 C/C++ Tricky Programs - GeeksforGeeks

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks, Some rights reserved

https://www.geeksforgeeks.org/c-cpp-tricky-programs/ 9/9

You might also like