Skip to content

Commit bf1e958

Browse files
authored
Add files via upload
GCD Calculation using Euclidean Algorithm in C++ This C++ program computes the Greatest Common Divisor (GCD) of two integers using the efficient Euclidean algorithm. The __gcd function from the <algorithm> library is utilized for simplicity. The code is useful in mathematical problems, string manipulation, and simplifying ratios or fractions.
1 parent b24591b commit bf1e958

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string gcdOfStrings(string str1, string str2) {
4+
5+
if(str1+str2==str2+str1)
6+
{
7+
// Calculate greate common divisor
8+
int gcdlength= __gcd(str1.length(), str2.length());
9+
// return the sub string
10+
return str1.substr(0, gcdlength);
11+
}
12+
// return null if the logic do not flow
13+
return "";
14+
}
15+
};

0 commit comments

Comments
 (0)