Saturday, May 2, 2015

Program to display a line of text in reverse order using recursive function


#include< stdio.h>
 #include< string.h>
 void rec(char *,int);
 void main()
 {
  char ch[100];
  int len;
  clrscr();
  printf("\nEnter the string:");
  gets(ch);
  len=strlen(ch);
  printf("\nSentence in reverse direction=");
  rec(ch,len-1);
  getch();
  }
  void rec(char *p,int n)
  {
   if(n==-1)
   return;
   printf("%c",*(p+n));
   n--;
   rec(p,n);
   }


   Write a recursive program to compute x^n

   long power (int, int);

void main()
{
    int p, n;
    clrscr();
    long result;

    printf("Enter 'n' and 'p': ");
    scanf("%d%d", &n, &p);
   result = power(n, p);
    printf("%d^%d is %ld", n, p, result);
    getch();
}

long power (int n, int p)
{
    int y,x;
    if (p==0)
    return 1;
    x=p-1;
   y=power(n,x);
   return n*y;
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner