Saturday, June 11, 2011

REVERSE A NUMBER USING RECURSION IN C PROGRAM

#include<stdio.h>
int main(){
int num,rev;
printf("\nEnter a number :");
scanf("%d",&num);
rev=reverse(num);
printf("\nAfter reverse the no is :%d",rev);
return 0;
}

int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}

FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM

#include<stdio.h>
int main(){
  int pow,num;
  long int res;
  long int power(int,int);
  printf("\nEnter a number: ");
  scanf("%d",&num);
  printf("\nEnter power: ");
  scanf("%d",&pow);
  res=power(num,pow);
  printf("\n%d to the power %d is: %ld",num,pow,res);
  return 0;
}
  int i=1;
  long int sum=1;
  long int power(int num,int pow){
      if(i<=pow){
           sum=sum*num;
          power(num,pow-1);
      }
      else
      return sum;
}

FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM

#include<stdio.h>
int main(){
  int num,x;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);
  return 0;
}

int r,s;
int findsum(int n){
if(n){
         r=n%10;
         s=s+r;
         findsum(n/10);
     }
     else
       return s;
}

FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM

#include<stdio.h>
int main(){
  int n1,n2,gcd;
  printf("\nEnter two numbers: ");
  scanf("%d %d",&n1,&n2);
  gcd=findgcd(n1,n2);
  printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
  return 0;
}

int findgcd(int x,int y){
     while(x!=y){
          if(x>y)
              return findgcd(x-y,y);
          else
             return findgcd(x,y-x);
     }
     return x;
}

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

C programming questions and answer

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

Division of large numbers in c

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000

char * division(char [],unsigned long);
int main(){
    char a[MAX];
    unsigned long b;
    char *c;
    printf("Enter the divdend : ");
    scanf("%s",a);
    printf("Enter the divisor : ");
    scanf("%lu",&b);
    c = division(a,b);
    printf("\nQuotient of the division : ");
    printf("%s",c);
    return 0;
}

char * division(char a[],unsigned long b){
    static char c[MAX];
    int la;
    int i,k=0,flag=0;
    unsigned long temp=1,reminder;
    la=strlen(a);

    for(i=0;i<=la;i++){
         a[i] = a[i] - 48;
    }

    temp = a[0];
    reminder = a[0];
    for(i=1;i<=la;i++){
         if(b<=temp){
             c[k++] = temp/b;
             temp = temp % b;
             reminder = temp;
             temp =temp*10 + a[i];
             flag=1;

         }
         else{
             reminder = temp;
             temp =temp*10 + a[i];
             if(flag==1)
                 c[k++] = 0;
         }
    }

    for(i=0;i<k;i++){
         c[i]=c[i]+48;
    }
    c[i]= '\0';

    printf("Reminder of division:  %lu  ",reminder);
    return c;
}