Dart int gcd()
Syntax & Examples
int.gcd() method
The `gcd` method in Dart returns the greatest common divisor of this integer and another integer.
Syntax of int.gcd()
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.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
other | required | The other integer to find the greatest common divisor with. |
Return Type
int.gcd() returns value of type int
.
✐ Examples
1 Find the greatest common divisor of two numbers
In this example,
- We assign the values
24
and36
to the integer variablesnum1
andnum2
respectively. - We find the greatest common divisor of
num1
andnum2
using thegcd()
method. - We print the result to standard output.
Dart Program
void main() {
int num1 = 24;
int num2 = 36;
int greatestCommonDivisor = num1.gcd(num2);
print('Greatest common divisor of $num1 and $num2: $greatestCommonDivisor');
}
Output
Greatest common divisor of 24 and 36: 12
2 Find the greatest common divisor of two numbers
In this example,
- We assign the values
56
and72
to the integer variablesnum1
andnum2
respectively. - We find the greatest common divisor of
num1
andnum2
using thegcd()
method. - We print the result to standard output.
Dart Program
void main() {
int num1 = 56;
int num2 = 72;
int greatestCommonDivisor = num1.gcd(num2);
print('Greatest common divisor of $num1 and $num2: $greatestCommonDivisor');
}
Output
Greatest common divisor of 56 and 72: 8
3 Find the greatest common divisor of two numbers
In this example,
- We assign the values
105
and140
to the integer variablesnum1
andnum2
respectively. - We find the greatest common divisor of
num1
andnum2
using thegcd()
method. - We print the result to standard output.
Dart Program
void main() {
int num1 = 105;
int num2 = 140;
int greatestCommonDivisor = num1.gcd(num2);
print('Greatest common divisor of $num1 and $num2: $greatestCommonDivisor');
}
Output
Greatest common divisor of 105 and 140: 35
Summary
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.