Data Structure using c Write a recursive function that finds
Data Structure using c++
Write a recursive function that finds the greatest common divisor of two positive integers. If any number is less than 0 then replace the number with its absolute value before computing the gcd. If any number is 0 then return 1 as the gcd.
Write a non recursive function that finds the least common multiple of two positive numbers. The least common multiple of two numbers is the product of the two numbers divided by their greatest common divisor, If any number is less 0 then replace the number with its absolute value before computing the lcm.
Solution
/* tested on c++ compiler */
#include <iostream>
using namespace std;
int gcd(int n1,int n2){ //Recursive function for calculating greatest common divisor
if((n1>=n2)&&(n1 % n2 ==0)) //base condition for the recursive function
return n2;
else
return gcd(n2,n1%n2); //function calling function itself i.e recursive
}
int lcm(int n1,int n2){ //Non recursive function for calculating least common multiple
int gcd1; //variable for storing greatest common divisor of n1 and n2
if(n1==0 || n2==0) //according to the condition if either of two numbers is zero then greatest common divisor=1
gcd1=1;
else
gcd1=gcd(n1,n2); //calling greatest common divisor function
int leastcommonmultiple=(n1*n2)/gcd1; //least common multiple=the product of the two numbers divided by their greatest common divisor
return leastcommonmultiple; //returning least common multiple of two numbers
}
int main() //main function
{
int grtstcomndivsr; //variable for storing greatest common divisor of n1 and n2
int n1,n2; //variable for storing value of two numbers
cout<<\"enter first number \ \"; //asking the user to enter first number
cin>>n1; //storing first number in the variable n1
cout<<\"enter second number \ \"; //asking the user to enter second number
cin>>n2; //storing second number in the variable n2
n1=abs(n1); //calling the absolute function which will convert the n1 into positive integer if //in case n1 is negative
n2=abs(n2); //calling the absolute function which will convert the n2 into positive integer if //in case n2 is negative
if(n1==0 || n2==0) //according to the condition if either of two numbers is zero then greatest common divisor=1
grtstcomndivsr=1;
else
grtstcomndivsr=gcd(n1,n2); //calling greatest common divisor function
cout<<\"greatest common divisor of two positive integers \"<<n1<<\" and \"<<n2<<\" = \"<<grtstcomndivsr<<\"\ \"; //output the greatest common divisor
cout<<\"least common multiple of two positive integers \"<<n1<<\" and \"<<n2<<\" = \"<<lcm(n1,n2); //output the least common multiple
return 0;
} //end of program
************OUTPUT*********
enter first number
2
enter second number
10
greatest common divisor of two positive integers 2 and 10 = 2
least common multiple of two positive integers 2 and 10 = 10
enter first number
-2
enter second number
10
greatest common divisor of two positive integers 2 and 10 = 2
least common multiple of two positive integers 2 and 10 = 10
************OUTPUT***********
Please let me know in case of any question

