Skip to main content

C-Programming Program 4(b)

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




Comments

Popular posts from this blog

C-Programming with Record

C-Programming Programs Syllabus Click for full record  1.a) Write a C program to ask the user to enter one char ( Upper-Case letter) check  whether user  entered a Upper-case letter or not(by using relational and logical operators) and then if user has  entered a Upper-case letter  convert into a Lower-case letter? ( hint: Upper-case means capital letters,  use ASCII information to check for Upper-case and convert) b) Write a  C program to ask the user to enter two integers and apply all arithmetic operations on those  print the corresponding values?(hint : +,-,*,/,%)     c) Write a C program to Determine the ranges of char, short, int and long int variables both signed and  Unsigned   By using sizeof operator(ii) By printing appropriate values from standard header (limits.h )   2.a) Write a Program to Find the Roots of a Quadratic Equation using if else and Switch statements. b) Write a Program which Gener...

OOPS CPP Program-14

14. Write a program to find transpose of a given matrix of mxn size using unary operator overloading #include<iostream> using namespace std; class matrix {         int x[5][5],m,n; public:         matrix(){ }         matrix(int a,int b)         {                m=a;n=b;         }         void get();         void put();         matrix operator !(); }; void matrix::get() {         int i,j;         cout<<"\n Enter a matrix of order"<<m<<"x"<<n<<"\n";                for(i=0;i<m;i++)                    for(j=0;j<n;j++)                        ...

OOPS CPP Program-15

15. Write a program to concatenate one string to another using binary operator overloading. #include<iostream> #include<string.h> using namespace std; class str  {         char *st; public:         str(int len, char *s)         {                  int l=strlen(s);                st= new char[l];                st=s;          }         str()         {         }         void put();         str operator +(str); }; str str :: operator +(str s)  {  /*str abc; abc.st=strcat(st, s.st); return abc;*/         st=strcat(st, s.st);         return *this; } void str :: put() {   cout<<...