Search This Blog

Translate This Page

Wednesday, 27 February 2013

Rotate bits of a Numbers


Left and Right Rotation of BIT:     


#include <stdio.h>
#define BIT 32
int leftrotate(int n,int d)
{
        return (n<<d)|(n>>(BIT-d));
}
int rightrotate(int n,int d)
{
        return (n>>d)|(n<<(BIT-d));
}
int main()
{
        int n,d;
        printf ("Enter no\n");
        scanf ("%d",&n);
        printf ("how many bit left rotate\n");
        scanf ("%d",&d);
        printf (" left rotation of %d by %d is ",n,d);
        printf ("%d\n",leftrotate(n,d));
        printf (" right rotation of %d by %d is ",n,d);
        printf ("%d\n",rightrotate(n,d));
}

output:

Enter no
4
how many bit left rotate
2
 left rotation of 4 by 2 is 16
 right rotation of 4 by 2 is 1

Print Reverse of a string using Recursion


Print reverse of a  string using recursion:


# include <stdio.h>

/* Function to print reverse of the passed string */
void reverse(char *str)
{
   if(*str)
   {
       reverse(str+1);
       printf("%c", *str);
   }
}

int main() 
{
   char a[] = "Tech Stuff for Interview";
   reverse(a);
   return 0;
}



C program to print all permutations of a given string



C program to print all permutations of a given string:



# include <stdio.h>
void swap (char *x, char *y)                //swap values at two pointers
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void permute(char *a, int i, int n)

{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j));                 //backtrack
       }
   }
}

int main()
{
   char a[] = "ABC";
   permute(a, 0, 2);
   return 0;
}

ASCII Table

Add caption
  

Implement Your Own sizeof Function

Implement Your Own sizeof () function:


#define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
int main()
{
    double x;
    printf("%d", my_sizeof(x));
    getchar();
    return 0;
}



Programme to find Nearest Vowel


This code is for finding nearest Vowel whatever Character you enter by your keyboard.

#include <stdio.h>
int main()
{
        int a[10]={65,69,73,79,85,97,101,105,111,117};  //Array of Vowel (a,e,i,o,u,A,E,I,O,U)
        char ch;
        int temp,temp1,i;
        printf ("enter character\n");
        scanf ("%c",&ch);
        temp=ch-a[0];
        for (i=0;i<10&&((ch-a[i])>=0);i++)
        {
                if( ch==a[i] )
                        {
                        printf ("nearest vowel is:  %c\n",a[i]);
                        return 0;
                        }
                if( temp>(ch-a[i]) )
                        {
                        temp1=a[i];
                        }

        }
        printf ("nearest vowel is %c\n",temp1);
        return 0;
}

Tuesday, 26 February 2013

Subtracting two numbers without using '-' operator

Subtracting two numbers without using '-' operator


#include <stdio.h>

int add(int a,int b)
{
int x;
x=a^b;
while(a&b)
{
b=((a&b)<<1);
a=x;
x=a^b;
//b=(a^b);
}
return x;
}
int sub(int a, int b) // add a with b's 2's complement.
{
return (add(a, add(~b, 1)));
}
int main() {
int a, b, res;
a = 3, b = 1;
printf ("Enter two no\n");
scanf ("%d %d",&a,&b);
res = sub(a, b);
printf("%d\n", res);
printf ("After Sub. is  no  %d",res);
return 0;
} 


output: 
Enter two no
10
5
After Sub. no is 5

Add two numbers without using arithmetic operators

Add two numbers without using arithmetic operators:


Method 1:

#include<stdio.h>
int Add(int x, int y)
{
    // Iterate till there is no carry  
    while (y != 0)
    {
        // carry now contains common set bits of x and y
        int carry = x & y;  

        // Sum of bits of x and y where at least one of the bits is not set
        x = x ^ y; 

        // Carry is shifted by one so that adding it to x gives the required sum
        y = carry << 1;
    }
    return x;
}

int main()
{
    printf("%d", Add(15, 32));
    return 0;
}


Method 2:  (Using for Loop)



int sum(int num1, int num2)
{
    int total, temp, temptotal;  
    total = num1 ^ num2;    
  
    for(temp = num1 & num2; temp != 0;temp = temptotal & temp) 
      {      
      temptotal = total;    
      total = total ^ (temp = temp << 1);  
      }
  
   return total;
}


Latest Current Visitor

World Clock Time, for View Click on Country