Saturday, June 11, 2011

FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM

#include<stdio.h>
int main(){
  int num,f;
  printf("\nEnter a number: ");
  scanf("%d",&num);
  f=fact(num);
  printf("\nFactorial of %d is: %d",num,f);
  return 0;
}

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

BINARY SEARCH THROUGH RECURSSION USING C PROGRAM

#include<stdio.h>
int main(){
  int a[10],i,n,m,c,l,u;
  printf("Enter the size of an array->");
  scanf("%d",&n);
  printf("\nEnter the elements of the array->");
  for(i=0;i<n;i++){
    scanf("%d",&a[i]);
  }
  printf("\nThe elements of an array are->");
  for(i=0;i<n;i++){
    printf(" %d",a[i]);
  }
  printf("\nEnter the number to be search->");
  scanf("%d",&m);
  l=0,u=n-1;
  c=binary(a,n,m,l,u);
  if(c==0)
    printf("\nThe number is not in the list");
  else
     printf("\nThe number is found");
   return 0;
 }

 int binary(int a[],int n,int m,int l,int u){
   int mid,c=0;
   if(l<=u){
     mid=(l+u)/2;
      if(m==a[mid]){
          c=1;
      }
      else if(m<a[mid]){
          return binary(a,n,m,l,mid-1);
      }
      else
          return binary(a,n,m,mid+1,u);
    }
   else
         return c;
  }

BINARY SEARCH USING C PROGRAM

#include<stdio.h>
int main(){
  int a[10],i,n,m,c=0,l,u,mid;
  printf("Enter the size of an array->");
  scanf("%d",&n);
  printf("\nEnter the elements of the array->");
  for(i=0;i<n;i++){
      scanf("%d",&a[i]);
  }
  printf("\nThe elements of an array are->");
  for(i=0;i<n;i++){
      printf(" %d",a[i]);
  }
  printf("\nEnter the number to be search->");
  scanf("%d",&m);
  l=0,u=n-1;
  while(l<=u){
      mid=(l+u)/2;
      if(m==a[mid]){
      c=1;
          break;
      }
      else if(m<a[mid]){
      u=mid-1;
      }
      else
      l=mid+1;
  }
  if(c==0)
      printf("\nThe number is not in the list");
  else
      printf("\nThe number is found");
  return 0;
}