A program to print numbers from 1 to 100 without using loops

Method1 (Using recursion)

void printUp(int startNumber, int endNumber) {   if (startNumber > endNumber)     return;     printf("[%d]\n", startNumber++);   printUp(startNumber, endNumber); }

Method2 (Using goto)

`
void printUp(int startNumber, int endNumber)
{
start:

    if (startNumber > endNumber)
    {
       goto end;
    }
    else
    {
       printf("[%d]\n”, startNumber++);
       goto start;
    }

end:
    return;
}`

How to add two numbers without using the plus operator?

Actually, SUM = A XOR B CARRY = A AND B Recursive: int add(int a, int b){ if (!a) return b; else return add((a & b) « 1, a ^ b); } Iterative **unsigned long add(unsigned long integer1, unsigned long integer2) { unsigned long xor, and, temp;  and = integer1 & integer2; /* Obtain the carry bits */ xor = integer1 ^ integer2; /* resulting bits */  while(and ! [Read More]

Convert the decimal number to any base (binary, hex, oct etc...)

#include 

int main()
{
  decimal_to_anybase(10, 2);
  decimal_to_anybase(255, 16);
  getch();
}

decimal_to_anybase(int n, int base)
{
  int i, m, digits[1000], flag;
  i=0;

  printf("\n\n[%d] converted to base [%d] : “, n, base);

  while(n)
  {
     m=n%base;
     digits[i]="0123456789abcdefghijklmnopqrstuvwxyz”[m];
     n=n/base;
     i++;
   }

   //Eliminate any leading zeroes
   for(i–;i>=0;i–)
   {
     if(!flag && digits[i]!='0’)flag=1;
     if(flag)printf("%c”,digits[i]);
   } 
}

Given an eight-bit bitmap graphics file, devise an algorithm to convert the file into a two-bit ASCII approximation

Assume that the file format is one byte for every pixel in the file, and that the approximation will produce one ASCII character of output for each pixel. This problem is easier to solve than it sounds. This is one of the tricks used in technical interview questions. Problems may be obscured or made to sound difficult. Don’t be fooled! Take the time to think about the core of the problem. [Read More]

Given the time, devise an algorithm to calculate the angle between the hour and minute hands of an analog clock

The important realization for this problem is that the hour hand is always moving. In other words, at 1:30, the hour hand is halfway between 1 and 2. Once you remember that, this problem is fairly straightforward. Assuming you don’t care whether the function returns the shorter or larger angle.

Swap two number in place without temporary variables.

Problem Write a function to swap two number in place without temporary variables. Solution Method1 - The XOR or Exclusive trick In C this should work: a ^= b ^= a ^= b; to simplify : a=a^b; b=a^b; a=a^b; OR a^=b; b^=a; a^=b; Following are operations in XOR 0^0=0 0^1=1 1^0=1 1^1=0 Hence, we have: a=a^b: ‘a’ will save all the bits that a differs from b: if the bit that ‘a’ and ‘b’ differ, it gets 1, otherwise 0. [Read More]