C++ or C program to find HCF or GCD of two numbers using Recursion/Recursive function

File name:- hcfrec.cpp
#include <iostream.h>
#include<conio.h>
int main(){
int n1, n2;
int hcf(int n1, int n2);
clrscr();
cout<<"Enter two positive integers"<<endl;
cin>>n1>>n2;
cout<<"H.C.F of "<<n1<<" and "<<n2<<" is= "<<hcf(n1,n2)<<endl;
getch();
return 0;
}
int hcf(int n1, int n2){
if (n2 != 0)
return hcf(n2, n1%n2);
else
return n1;
}

File name:- hcfrec.c
#include <stdio.h>
#include<conio.h>
int main(){
int n1, n2;
int hcf(int n1, int n2);
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("H.C.F of %d and %d is %d.", n1, n2, hcf(n1,n2));
getch();
return 0;
}
int hcf(int n1, int n2){
if (n2 != 0)
return hcf(n2, n1%n2);
else
return n1;
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...