-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.cpp
More file actions
41 lines (30 loc) · 787 Bytes
/
mod.cpp
File metadata and controls
41 lines (30 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;
int main() {
long long base, exponent, modulus;
cout << "Enter base: ";
cin >> base;
cout << "Enter the value of the exponent: ";
cin >> exponent;
cout << "Enter the value of modulus (positive): ";
cin >> modulus;
if (modulus <= 0) {
cout << "Modulus must be positive.\n";
return 0;
}
if (exponent < 0) {
cout << "Exponent must be non-negative.\n";
return 0;
}
long long result = 1;
base = base % modulus;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = (result * base) % modulus;
}
base = (base * base) % modulus;
exponent /= 2;
}
cout << "Result: " << result << endl;
return 0;
}