4(b)Write C programs that use both recursive and non-recursive functions
i) To find the factorial of a given integer.
ii) To find the GCD (greatest common divisor) of two given integers.
iii) To solve Towers of Hanoi problem.
Algorithms
Factorial using non recursion
Step-1 : Start
Step-2 : Declare variables n,fact
Step-3 : Read vaue for n
Step-4 : Call the function with argument n and result is in fact
4.1 : Declare variables fact with in function
4.2 : initialize fact value to 1
4.3 : Initialise i value to 0
4.4 : if(i<=n)
4.4.1 : fact=fact*i;
4.5 : Increment value of i and go to step-4.4
4.6 : return fact and value is returned to called function
Step-5 : Display factorial value
Step-6 : Stop
Factorial using recursion
Step-1 : Start
Step-2 : Declare variables n,fact
Step-3 : Read vaue for n
Step-4 : Call the function factorial with argument n and result is in fact
4.1 : if(n= =0)
4.1.1 : return 1 to called function
4.2 : if(n!=0)
4.2.1 : return(factorial(n*(n-1)) to called function
Step-5 : Display factorial value
Step-6 : Stop
GCD using recursion
Step-1 : Start
Step-2 : Declare variables a,b,c,min,max
Step-3 : Read values of a and b
Step-4 : find the minimum number min=a<b?a:b
Step-5 : find the maximum number max=a>b?a:b
Step-6 : Call the function GCD with two arguments min,max
6.1 : if(c= =0)
6.1.1 : return min TO called function
6.2 : if(c!=0)
6.2.1 : c=max%min
6.2.2 : return GCD(min,c) to called function
Step-7 : Display GCD value
Step-8 : Stop
GCD using non-recursion
Step-1 : Start
Step-2 : Declare variables a,b,c,min,max
Step-3 : Read values of a and b
Step-4 : find the minimum number min=a<b?a:b
Step-5 : find the maximum number max=a>b?a:b
Step-6 : Call the function GCD with two arguments min,max
6.1 : Declare variable c
6.2 : if((min<max)&&(c!=0))
6.2.1 : c=max%min
6.2.2 : max=min
6.2.3 : min=c
6.2.4 : Go to step-6.2
6.3 : Return max value to called function
Step-7 : Display value of GCD
Step-8 : Stop
Flowcharts
4 (b) -1
Rec & Non Rec
4(b) - 2
Programs
4 (b) -1
/*Write C programs that use both recursive and non-recursive functions*/
#include<stdio.h>
int rec_factorial(int n);
int iter_factorial(int n);
main()
{
int i,n;
int fact;
printf("enter the number" );
scanf("%d",&n);
printf("rec is %d ",rec_factorial(n));
printf(" non recursive %d",iter_factorial(n));
}
int rec_factorial(int n)
{
return n>1?n*rec_factorial(n-1):1;
}
int iter_factorial(int n)
{
int fact=1;
while(n>1)
{
fact=fact*n;
n=n-1;
}
return fact;
}
Program
4(b) -2
/* Write C programs that use both recursive and non-recursive functions
To find the GCD (greatest common divisor) of two given integers.*/
#include<stdio.h>
#include<math.h>
int gcd_rec(int,int);
int gcd_nonRecursive(int,int);
int main()
{
int a,b,c;
printf(" enter the input to calculate GCD\n");
scanf("%d%d",&a,&b);
printf(" recursive : %d\n",gcd_rec(a,b));
printf(" non recursive : %d\n", gcd_nonRecursive(a,b));
}
int gcd_rec(int m,int n)
{
if(n>m)
return gcd_rec(n,m);
{
it(n==0)
return m;
else
return gcd_rec(n,m%n);
}
}
int gcd_nonRecursive(int a,int b)
{
int c;
if (a < b)
{
c = a;
a = b;
b = c;
}
while(1)
{
c = a%b;
if(c==0)
return b;
a = b;
b = c;
}
Program
4 (b) - 3
/*Write a C program to solve the Towers of Hanoi problem using Recursive function.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
printf("Move Disk From %c to %c\n",from,to);
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main( )
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch( );
}
i) To find the factorial of a given integer.
ii) To find the GCD (greatest common divisor) of two given integers.
iii) To solve Towers of Hanoi problem.
Algorithms
Factorial using non recursion
Step-1 : Start
Step-2 : Declare variables n,fact
Step-3 : Read vaue for n
Step-4 : Call the function with argument n and result is in fact
4.1 : Declare variables fact with in function
4.2 : initialize fact value to 1
4.3 : Initialise i value to 0
4.4 : if(i<=n)
4.4.1 : fact=fact*i;
4.5 : Increment value of i and go to step-4.4
4.6 : return fact and value is returned to called function
Step-5 : Display factorial value
Step-6 : Stop
Factorial using recursion
Step-1 : Start
Step-2 : Declare variables n,fact
Step-3 : Read vaue for n
Step-4 : Call the function factorial with argument n and result is in fact
4.1 : if(n= =0)
4.1.1 : return 1 to called function
4.2 : if(n!=0)
4.2.1 : return(factorial(n*(n-1)) to called function
Step-5 : Display factorial value
Step-6 : Stop
GCD using recursion
Step-1 : Start
Step-2 : Declare variables a,b,c,min,max
Step-3 : Read values of a and b
Step-4 : find the minimum number min=a<b?a:b
Step-5 : find the maximum number max=a>b?a:b
Step-6 : Call the function GCD with two arguments min,max
6.1 : if(c= =0)
6.1.1 : return min TO called function
6.2 : if(c!=0)
6.2.1 : c=max%min
6.2.2 : return GCD(min,c) to called function
Step-7 : Display GCD value
Step-8 : Stop
GCD using non-recursion
Step-1 : Start
Step-2 : Declare variables a,b,c,min,max
Step-3 : Read values of a and b
Step-4 : find the minimum number min=a<b?a:b
Step-5 : find the maximum number max=a>b?a:b
Step-6 : Call the function GCD with two arguments min,max
6.1 : Declare variable c
6.2 : if((min<max)&&(c!=0))
6.2.1 : c=max%min
6.2.2 : max=min
6.2.3 : min=c
6.2.4 : Go to step-6.2
6.3 : Return max value to called function
Step-7 : Display value of GCD
Step-8 : Stop
Flowcharts
4 (b) -1
Rec & Non Rec
4(b) - 2
Programs
4 (b) -1
/*Write C programs that use both recursive and non-recursive functions*/
#include<stdio.h>
int rec_factorial(int n);
int iter_factorial(int n);
main()
{
int i,n;
int fact;
printf("enter the number" );
scanf("%d",&n);
printf("rec is %d ",rec_factorial(n));
printf(" non recursive %d",iter_factorial(n));
}
int rec_factorial(int n)
{
return n>1?n*rec_factorial(n-1):1;
}
int iter_factorial(int n)
{
int fact=1;
while(n>1)
{
fact=fact*n;
n=n-1;
}
return fact;
}
Program
4(b) -2
/* Write C programs that use both recursive and non-recursive functions
To find the GCD (greatest common divisor) of two given integers.*/
#include<stdio.h>
#include<math.h>
int gcd_rec(int,int);
int gcd_nonRecursive(int,int);
int main()
{
int a,b,c;
printf(" enter the input to calculate GCD\n");
scanf("%d%d",&a,&b);
printf(" recursive : %d\n",gcd_rec(a,b));
printf(" non recursive : %d\n", gcd_nonRecursive(a,b));
}
int gcd_rec(int m,int n)
{
if(n>m)
return gcd_rec(n,m);
{
it(n==0)
return m;
else
return gcd_rec(n,m%n);
}
}
int gcd_nonRecursive(int a,int b)
{
int c;
if (a < b)
{
c = a;
a = b;
b = c;
}
while(1)
{
c = a%b;
if(c==0)
return b;
a = b;
b = c;
}
Program
4 (b) - 3
/*Write a C program to solve the Towers of Hanoi problem using Recursive function.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void hanoi(int x, char from, char to, char aux)
{
if(x==1)
printf("Move Disk From %c to %c\n",from,to);
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
}
void main( )
{
int disk;
int moves;
clrscr();
printf("Enter the number of disks you want to play with:");
scanf("%d",&disk);
moves=pow(2,disk)-1;
printf("\nThe No of moves required is=%d \n",moves);
hanoi(disk,'A','C','B');
getch( );
}



Comments
Post a Comment