Saturday, July 24, 2021
Home coding Write a program to GCD of two number.

Write a program to GCD of two number.

Given a two number we have to write a program to calculate GCD of two number.

So what is GCD of number?

GCD of two number is the largest number that divides both of them,

Example:

input:36 and 60
output:12

Logic:

we have to just find the largest number that divide both of them.

Code-

#include<bits/stdc++.h>
using namespace std;
int gcd(int a, int b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b); 
	
}
int main()
{
	int a = 36;
	int b = 60;
	cout<<gcd(a, b)<<endl;

}

Output:

12

Time complexity:0(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments