Problem statement:- Program to Find GCD of two numbers using recursion.
Data requirement:-
Input Data:- num1, num2
Output Data:-gcd(num1,num2)
Output Data:-gcd(num1,num2)
Program in C
Here is the source code of the C Program to Find GCD of two numbers using recursion.
Code:
#include<stdio.h>
int gcd(int num1, int num2)
{
if(num2==0)
return num1;
return gcd(num2,num1%num2);
}
int main()
{
int num1,num2;
printf("Enter the Two Number:");
scanf("%d%d",&num1,&num2);
printf("Gcd of Given Numbers Using Recursion is:%d",gcd(num1,num2));
}
Enter the Two Number:10 15
Gcd of Given Numbers Using Recursion is:5
Program in C++
Here is the source code of the C++ Program to Find GCD of two numbers using recursion.
Code:
#include<iostream>
using namespace std;
int gcd(int num1, int num2)
{
if(num2==0)
return num1;
return gcd(num2,num1%num2);
}
int main()
{
int num1,num2;
cout<<"Enter the Two Number:";
cin>>num1>>num2;
cout<<"Gcd of Given Number Using Recursion is:"<<gcd(num1,num2);
}
Enter the Two Number:22 74
Gcd of Given Number Using Recursion is:2
Program in Java
Here is the source code of the Java Program to Find GCD of two numbers using recursion.
Code:
import java.util.Scanner;
public class GcdOfGivenNumbers {
static int gcd(int num1, int num2)
{
if(num2==0)
return num1;
return gcd(num2,num1%num2);
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1, num2;
System.out.print("Enter the two Number:");
num1=cs.nextInt();
num2=cs.nextInt();
System.out.print("Gcd of Given Numbers Using Recursion is: "+gcd(num1,num2));
cs.close();
}
}
Enter the two Number:44 55
Gcd of Given Numbers Using Recursion is: 11
Program in Python
Here is the source code of the Python Program to Find GCD of two numbers using recursion.
Code:
def gcd(num1,num2):
if num2==0:
return num1
else:
return gcd(num2,num1%num2)
print("Enter the two Number:")
num1=int(input())
num2=int(input())
print("Gcd of Given Numbers Using Recursion is:",gcd(num1,num2))
Enter the two Number:
75
1075
Gcd of Given Numbers Using Recursion is: 25
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box