Dart int modPow()
Syntax & Examples
int.modPow() method
The `modPow` method in Dart returns this integer raised to the power of an exponent modulo a modulus.
Syntax of int.modPow()
The syntax of int.modPow() method is:
int modPow(int exponent int modulus)
This modPow() method of int returns this integer to the power of exponent modulo modulus.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
exponent | required | The exponent to raise this integer to. |
modulus | required | The modulus with which to take the result modulo. |
Return Type
int.modPow() returns value of type int
.
✐ Examples
1 Calculate power modulo with small numbers
In this example,
- We set
base
to 5,exponent
to 3, andmodulus
to 7. - We calculate
base
raised to the power ofexponent
modulomodulus
using themodPow()
method. - We print the result to standard output.
Dart Program
void main() {
int base = 5;
int exponent = 3;
int modulus = 7;
int result = base.modPow(exponent, modulus);
print('Result of $base^$exponent mod $modulus: $result');
}
Output
Result of 5^3 mod 7: 6
2 Calculate power modulo with medium numbers
In this example,
- We set
base
to 7,exponent
to 2, andmodulus
to 11. - We calculate
base
raised to the power ofexponent
modulomodulus
using themodPow()
method. - We print the result to standard output.
Dart Program
void main() {
int base = 7;
int exponent = 2;
int modulus = 11;
int result = base.modPow(exponent, modulus);
print('Result of $base^$exponent mod $modulus: $result');
}
Output
Result of 7^2 mod 11: 5
3 Calculate power modulo with small numbers
In this example,
- We set
base
to 3,exponent
to 4, andmodulus
to 5. - We calculate
base
raised to the power ofexponent
modulomodulus
using themodPow()
method. - We print the result to standard output.
Dart Program
void main() {
int base = 3;
int exponent = 4;
int modulus = 5;
int result = base.modPow(exponent, modulus);
print('Result of $base^$exponent mod $modulus: $result');
}
Output
Result of 3^4 mod 5: 1
Summary
In this Dart tutorial, we learned about modPow() method of int: the syntax and few working examples with output and detailed explanation for each example.