The `gcd` method in Dart returns the greatest common divisor of this integer and another integer.
The syntax of int.gcd() method is:
int gcd(int other)
This gcd() method of int returns the greatest common divisor of this integer and other.
Parameter | Optional/Required | Description |
---|---|---|
other | required | The other integer to find the greatest common divisor with. |
int.gcd() returns value of type int
.
In this example,
24
and 36
to the integer variables num1
and num2
respectively.num1
and num2
using the gcd()
method.void main() {
int num1 = 24;
int num2 = 36;
int greatestCommonDivisor = num1.gcd(num2);
print('Greatest common divisor of $num1 and $num2: $greatestCommonDivisor');
}
Greatest common divisor of 24 and 36: 12
In this example,
56
and 72
to the integer variables num1
and num2
respectively.num1
and num2
using the gcd()
method.void main() {
int num1 = 56;
int num2 = 72;
int greatestCommonDivisor = num1.gcd(num2);
print('Greatest common divisor of $num1 and $num2: $greatestCommonDivisor');
}
Greatest common divisor of 56 and 72: 8
In this example,
105
and 140
to the integer variables num1
and num2
respectively.num1
and num2
using the gcd()
method.void main() {
int num1 = 105;
int num2 = 140;
int greatestCommonDivisor = num1.gcd(num2);
print('Greatest common divisor of $num1 and $num2: $greatestCommonDivisor');
}
Greatest common divisor of 105 and 140: 35
In this Dart tutorial, we learned about gcd() method of int: the syntax and few working examples with output and detailed explanation for each example.