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

ParameterOptional/RequiredDescription
exponentrequiredThe exponent to raise this integer to.
modulusrequiredThe 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,

  1. We set base to 5, exponent to 3, and modulus to 7.
  2. We calculate base raised to the power of exponent modulo modulus using the modPow() method.
  3. 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,

  1. We set base to 7, exponent to 2, and modulus to 11.
  2. We calculate base raised to the power of exponent modulo modulus using the modPow() method.
  3. 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,

  1. We set base to 3, exponent to 4, and modulus to 5.
  2. We calculate base raised to the power of exponent modulo modulus using the modPow() method.
  3. 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.