Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C/C++ Tricky Programs
Here are 10 tricky C programming challenges that will test your understanding of language fundamentals and creative problem-solving techniques.
1. Program to Print Double Quotes in C
Printing double quotes in C requires escape sequences since quotes are used to delimit string literals. We use the backslash escape sequence " to print quotes −
#include <stdio.h>
int main() {
printf(""Tutorials Point "");
return 0;
}
"Tutorials Point "
2. Print Numbers 1 to 10 Without Loops or Goto
When loops and goto statements are forbidden, recursion provides an elegant solution for iteration −
#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
3. Check Equality Without Arithmetic or Comparison Operators
The XOR bitwise operator provides a clever way to check equality. If two numbers are equal, their XOR result is 0 −
#include <stdio.h>
int main() {
int a = 132;
int b = 132;
if (a ^ b)
printf("a is not equal to b");
else
printf("a is equal to b");
return 0;
}
a is equal to b
4. Print "Hello" Without Using Semicolon
The printf() function returns the number of characters printed. We can exploit this return value in conditional statements −
#include <stdio.h>
int main() {
if (printf("Hello "))
return 0;
}
Hello
5. Find Maximum and Minimum Without Comparison Operators
Using the absolute value function abs(), we can determine max and min through mathematical manipulation −
#include <stdio.h>
#include <stdlib.h>
int main() {
int x = 15, y = 20;
printf("The numbers are x = %d and y = %d\n", x, y);
printf("The max of the numbers is %d\n", ((x + y) + abs(x - y)) / 2);
printf("The min of the numbers is %d\n", ((x + y) - abs(x - y)) / 2);
return 0;
}
The numbers are x = 15 and y = 20 The max of the numbers is 20 The min of the numbers is 15
6. Print Source Code of the Program
This self-referencing program uses the __FILE__ macro to open and print its own source code −
#include <stdio.h>
int main(void) {
FILE *program;
char ch;
program = fopen(__FILE__, "r");
if (program == NULL) {
printf("Error opening file");
return 1;
}
do {
ch = fgetc(program);
printf("%c", ch);
} while (ch != EOF);
fclose(program);
return 0;
}
7. Sum Two Numbers Without Plus Operator
Mathematical manipulation using subtraction and negation can achieve addition −
#include <stdio.h>
int main() {
int x = 5;
int y = 5;
int sum = x - (-y);
printf("The numbers are x = %d y = %d\n", x, y);
printf("Their sum = %d", sum);
return 0;
}
The numbers are x = 5 y = 5 Their sum = 10
8. Check Even Number Without Arithmetic or Relational Operators
Bitwise AND with 1 checks the least significant bit. If it's 1, the number is odd; if 0, it's even −
#include <stdio.h>
int main() {
int a = 154;
if (a & 1) {
printf("%d is an odd number", a);
} else {
printf("%d is an even number", a);
}
return 0;
}
154 is an even number
9. Divide by 4 Without Division Operator
Right shift by 2 positions effectively divides by 4 (since 4 = 2²) −
#include <stdio.h>
int main() {
int n = 128;
printf("%d divided by 4 = ", n);
n = n >> 2;
printf("%d", n);
return 0;
}
128 divided by 4 = 32
10. Recursive Sum of Digits Until Single Digit
This uses a mathematical property: the recursive sum of digits equals (n-1) % 9 + 1 for positive numbers −
#include <stdio.h>
int main() {
int a = 534;
int sum;
if (a)
sum = a % 9 == 0 ? 9 : a % 9;
else
sum = 0;
printf("The final sum is %d", sum);
return 0;
}
The final sum is 3
Conclusion
These tricky programs demonstrate creative problem-solving in C using bitwise operations, recursion, mathematical properties, and language-specific features. They showcase how constraints can lead to innovative programming solutions.
