Pages

Visitors

Thursday 5 January 2012

Binary Search


#include <stdio.h>

main()

{
    int a[30],n,i,t,low,mid,high,found = 0;

    printf("\n enter the number of elements in array");

    scanf("%d",&n);

    if(n>30)

    {
        printf("\n too many numbers");
        exit(0);
    }

printf("\n enter elements of array: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("\n enter element to search: ");

scanf("%d",&t);

low = 0;

high = n-1;

while (high >= low)

{
    mid = (low+high)/2;

    if (a[mid] == t)
    {
        found = 1; break;
    }

    else if (t<a[mid])

             high = mid-1;

             else low = mid + 1;
}

if (found ==0)

printf("\n not found");

else printf("\n found at %d",mid);

getch();
}

Bubble Sort


#include <stdio.h>

main()

{
     int a[30],n,i,j,temp;

     printf("\n how many numbers");

     scanf("%d",&n);

     if (n>30)

     {
         printf("\n too many numbers");


     }

     printf("\n enter the array elements: ");

     for (i=0;i<n;i++)
     scanf("%d",&a[i]);

     for (i=0;i<n-1;i++)

     for(j=0;j<(n-i)-1;j++)

     if (a[j]>a[j+1])

     {
         temp = a[j];

         a[j] = a[j+1];
         a[j+1] = temp;
     }

     printf("The numbers in sorted order are : \n");

     for(i=0;i<n;++i)

     printf("\n%d",a[i]);

     getch();
}

string compare without strcmp()


#include <stdio.h>
#include <conio.h>
#include <string.h>

main()

{
    char s1[30],s2[20];

   int i,length = 0,temp;

   printf("enter string 1: ");
   gets(s1);

   printf("\n enter string 2: ");

   gets(s2);

   for(i=0;s1[i]!='\0';i++)

   {
       if(s1[i]== s2[i])

       temp = 1; else temp = 0;
   }

   if (temp == 1)

   printf("both the strings are same");

   else
   printf("both strings are not same.");
    getch();
}

string compare with strcmp()


#include <stdio.h>
#include <conio.h>
#include <string.h>

main()

{
    char s1[20],s2[20];

    int result;

    printf("\n enter first string: ");

    gets(s1);

    printf("\n enter second string: ");

    gets(s2);

    result = strcmp(s1,s2);

    if(result == 0)

    printf("\n both strings are equal");

    else
    printf("\n both strings are not equal");

    getch();
}

String concatenation without strcat()


#include <stdio.h>
#include <conio.h>
#include <string.h>

main()

{
    char s1[30],s2[20];

   int i,length = 0,temp;

   printf("enter string 1: ");
   gets(s1);

   printf("\n enter string 2: ");

   gets(s2);

   for(i=0;s1[i]!='\0';i++)

   length++;

   temp = length;

   for (i=0; s2[i]!= '\0';i++)

   {
       s1[temp] = s2[i];

       temp++;
   }

   s1[temp] = '\0';

   printf("\nThe concatenated string is    \n");

   puts(s1);
    getch();
}

string concatenation by strcat()


#include <stdio.h>
#include <conio.h>
#include <string.h>

main()

{
    char s1[20],s2[20];

    printf("\n enter first string");

    gets(s1);

    printf("\n enter second string");

    gets(s2);

    strcat(s1,s2);

    printf("\n The concatenated string is %s",s1);

    getch();
}

fibonacci by recursion


#include <stdio.h>
#include <conio.h>

int fib(int ,int , int , int);

main()

{
    int n,f,x=0,y=1,i=3;

    printf("\n enter length of series : ");

    scanf("%d",&n);

    printf("\n%d\t%d",x,y);

    fib(x,y,n,i);

    getch();
}


fib(int x, int y, int n, int i)

{
    int z;
    if (i<=n)

    {
        z = x+y; printf("\t%d",z);

        x = y;

        y =z;

        i++;

        fib(x,y,n,i);

    }
}

Call by reference swap



#include <stdio.h>
#include <conio.h>

void swap(int *x,int *y)

{
     int temp;
     temp = *x;
     *x=*y;
     *y=temp;
     printf("swapped values are a= %d and b = %d",*x,*y);
}

void main()

{
     int a = 7, b = 4;

     swap(&a,&b);

     printf("Original values are a= %d and b = %d",a,b);

     printf("The values after swap are a = %d,b = %d",a,b);


     getch();
}


factorial by recursion


#include <stdio.h>
#include <conio.h>

long fact(int);

main()

{
    int n;

    long f;

    printf("\n enter a number");
    scanf("%d",&n);
    f = fact(n);
    printf("\n Factorial of %d is %ld",n,f);

    getch();
}


long fact(int n)

{
    int m;

    if (n==1)

    return n;

    else
    {
        m = n*fact(n-1);
        return m;
    }
}

Call By Value Swapping


#include <stdio.h>
#include <conio.h>

void swap(int x,int y)

{
     int temp;
     temp = x;
     x=y;
     y=temp;
     printf("swapped values are a= %d and b = %d",x,y);
}

void main()

{
     int a = 7, b = 4;

     swap(a,b);

     printf("Original values are a= %d and b = %d",a,b);

     printf("The values after swap are a = %d,b = %d",a,b);


     getch();
}