Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

C / C++ Program to find the HCF / GCD of two numbers using recursion (recursive function)

Leave a Comment
/*  The following program identifies the HCF / GCD (Highest Common Factor / Greatest Common Divider) using the concept of recursion */

#include<stdio.h>
#include<conio.h>
int hcf(int,int);
// function prototyping
int main()
{
int a,b,gcd;
printf("Enter 2 Nos. to calculate HCF: \n");
scanf("%d%d",&a,&b);
gcd=hcf(a,b);
printf("GCD / HCF is : %d",gcd);
getch();
}
int hcf(int x,int y)
//function definition
{
if (y!=0)
return hcf(y,x%y);
else
return x;
}



Screenshot:
C / C++ Program to find the HCF / GCD of two numbers using recursion  (recursive  function) : Highest Common Factor / Greatest Common Divider
C / C++ Program to find the HCF / GCD of two numbers using recursion  (recursive  function)

Read More...

C++ Program To Find Factorial Of A Number, Using Recursive Function

Leave a Comment
/*Finding Factorial of a number using Recursion(or Recursive Function)
Recursive Function : Recursive function is a type of function which calls itself in Function Definition Code Segment until a terminating condition reached. In the following program "fact( )" is a recursive function.*/

#include<iostream.h>
#include<conio.h>

int fact(int n)               //Defining a recursive function for n! calculation 
{
int t;
if(n==1)
return (1);
else
t=n*fact(n-1);
return (t);
}
 
void main()
{
clrscr();
int n;
printf("\nEnter the number");
scanf("%d",&n);
printf("\n factorial=%d",
fact(n));
getche();
}
Read More...