/*Factorial of a number 'n' i.e below 8 using recursion */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int fact(int n); /* function declaration */
void main()
{
int n,f;
clrscr();
printf(“enter any value:”);
scanf(“%d”,&n);
f=fact(n); /* calling function */
printf(“\n factorial of %d is %d”,n,f);
getch();
}
#include<conio.h>
#include<string.h>
#include<math.h>
int fact(int n); /* function declaration */
void main()
{
int n,f;
clrscr();
printf(“enter any value:”);
scanf(“%d”,&n);
f=fact(n); /* calling function */
printf(“\n factorial of %d is %d”,n,f);
getch();
}
int fact (int n) /* called function */
{
int f;
if(n==1)
return 1;
f=n*fact(n-1);
return (f);
}
{
int f;
if(n==1)
return 1;
f=n*fact(n-1);
return (f);
}
Output:
/*Factorial of a number 'n' i.e above 8 using recursion */
#include<stdio.h>
#include<conio.h>
long factr(int); /* function declaration */
void main()
{
long fact;
int n;
clrscr();
printf("enter any value:\n");
scanf("%d",&n);
fact=factr(n); /* calling function */
printf("factorial of %d = %ld",n,fact);
getch();
}
long factr(n) /* called function */
{
long fac;
if(n==1)
return 1;
fac=n*factr(n-1);
return (fac);
}
/*Multiplication of two numbers using recursion*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include <math.h>
int mult (int,int) /* function declaration */
void main()
{
int a,b,c;
clrscr();
printf(“enter any two numbers:”);
scanf(“%d%d”,&a,&b);
c=mult(a,b); /* calling function */
printf(“multiplication of %d and %d is %d”,a,b,c);
getch();
}
#include<conio.h>
#include<string.h>
#include <math.h>
int mult (int,int) /* function declaration */
void main()
{
int a,b,c;
clrscr();
printf(“enter any two numbers:”);
scanf(“%d%d”,&a,&b);
c=mult(a,b); /* calling function */
printf(“multiplication of %d and %d is %d”,a,b,c);
getch();
}
int mult (int a,int b) /* called function */
{
if(b==1)
return a;
c=b+mult(a,b-1);
return (c);
}
{
if(b==1)
return a;
c=b+mult(a,b-1);
return (c);
}
Output:
/*Power of two numbers using recursion*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int a,b,c;
int pow(int,int);
void main()
{
int c;
clrscr();
printf(“enter any two numbers:”);
scanf(“%d%d,&a,&b);
c=pow(a,b);
printf(“\n power of %d and %d is %d”,a,b,c);
getch();
}
#include<conio.h>
#include<string.h>
#include<math.h>
int a,b,c;
int pow(int,int);
void main()
{
int c;
clrscr();
printf(“enter any two numbers:”);
scanf(“%d%d,&a,&b);
c=pow(a,b);
printf(“\n power of %d and %d is %d”,a,b,c);
getch();
}
int pow (int a,int b)
{
int c;
if(b==0)
return 1;
c=a*pow(a,b-1);
return (c);
}
{
int c;
if(b==0)
return 1;
c=a*pow(a,b-1);
return (c);
}
Output:
/*GCD of two numbers using recursion*/
#include<stdio.h>
#include<conio.h>
int gcd(int,int); /* function declaration */
void main()
{
int a,b,c;
clrscr();
printf("\n enter any two values:\n");
scanf("%d%d",&a,&b);
c=gcd(a,b); /* calling function */
printf("GCD of %d and %d = %d",a,b,c);
getch();
}
int gcd(int m,int n) /* called function */
{ int x;
if(n==0)
return m;
if(n>m)
return (gcd(n,m));
else
x=m%n;
return(gcd(n,x));
}
/* Fabinacci series using recursion*/
#include<stdio.h>
#include<conio.h>
int n,fib; /* global variable */
int fibon(int); /* function declaration */
void main()
{
int i;
clrscr();
printf("enter any value:\n");
scanf("%d",&n);
printf("\n Fabinacci series of %d: \n",n);
for(i=0;i<n;i++)
{
fib=fibon(i); /* calling function */
printf("%d\t",fib);
}
getch();
}
int fibon(n) /* called function */
{
int a,b;
if(n<=1)
return n;
a=fibon(n-1);
b=fibon(n-2);
return (a+b);
}
Output:
/* Towers of Hanoi using recursion */
#include<stdio.h>
#include<conio.h>
void hanoi(int,char,char,char); /* function declaration */
void main()
{
int n;
clrscr();
printf("\n Enter no. of disks to be moved:\n");
scanf("%d",&n);
hanoi(n,'X','Z','Y');
getch();
}
void hanoi(int n,char initial,char final,char temp)
{
if(n==1)
{
printf("move disk 1 from needle %c to %c\n",initial,final);
return;
}
hanoi(n-1,initial,temp,final);
printf("move disk %d from needle %c to %c\n",n,initial,final);
hanoi(n-1,temp,final,initial);
}
No comments:
Post a Comment