Tricky Programs

 



1.       Print “Hello” without using a ;(semi-colon) in c/c++

 

#include <stdio.h>

int main(){

   if(printf("Hello "))

   return 0;

}

 

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 (^). For two equal numbers XOR operator returns 0 as all bits would be same and XOR of equal bits is 0.

#include <iostream>

using namespace std;

int main()

{

   int x = 10;

   int y = 10;

   if (!(x ^ y))

      cout << " x is equal to y ";

   else

      cout << " x is not equal to y ";

   return 0;

}

 

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

#include<iostream>

using namespace std;

int main(){

   int a = 154;

   if(a & 1) {

      cout<<a<<" is an odd number";

   } else{

      cout<<a<<" is an even number";

   }

   printf("\n");

   return 0;

}


 

4.        C++ 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;

}

 

5.       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;

}

 

6. CPP program to print sum of two integers withtout + !! Simple Trick..

 

#include<iostream>

using namespace std;

int main()

{

int a = 5;

int b = 5;

int sum = -( -a-b );

cout << sum;

return 0;

}

 

 

7.       CPP program to divide a number by 4 (or any multiples of 2) without using '/'

 

#include<iostream>

using namespace std;

int main()

{

 int n = 4;

 n = n >> 2;

 cout << n;

 return 0;

}

 

 
8.       Print the source code of program and output 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.

 

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

}

Comments

Popular posts from this blog

Components of a Research Paper

Data Preprocessing

Artificial Intelligence - An Overview by Dharaneish