-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodExpo.java
More file actions
49 lines (45 loc) · 1.4 KB
/
Copy pathmodExpo.java
File metadata and controls
49 lines (45 loc) · 1.4 KB
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
42
43
44
45
46
47
48
49
import java.util.*;
public class modExpo {
public static int k = 0;
public static int modularExp(int b, int[] bi, int m){
int res = 1;
int power = b % m;
for(int i=0; i < bi.length; i++){
if(bi[i]==1)
res = (res*power) % m;
power = (power*power) % m;
}
return res;
}
public static int[] intToBin(int x){
int num = x;
int size = 10;
int[] binary;
binary = new int[size];
int[] temp = new int[size];
while(num != 0){
temp[k] = num % 2;
num = num / 2;
k++;
}
return temp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter base number : ");
int base = sc.nextInt();
System.out.println("Enter exponent number : ");
int expo = sc.nextInt();
System.out.println("Enter modulus number : ");
int mod = sc.nextInt();
int[] bin ;
bin = intToBin(expo);
System.out.print("Binary of " + expo + " = ");
for(int i =k; i >= 0; i--){
System.out.print(bin[i]);
}
System.out.println();
int res = modularExp(base,bin,mod);
System.out.println("Result : " + base + "^" + expo + " % " + mod + " = " + res);
}
}