-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany-base-addition.java
51 lines (40 loc) · 1.3 KB
/
any-base-addition.java
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
50
51
// Any Base Addition Solution
import java.util.*;
public class Main{
// Function to add numbers in any base system
public static int getSum(int b, int n1, int n2){
// convert n1 & n2 to dec system
int decN1= getValueInDecimal(n1, b);
int decN2= getValueInDecimal(n2, b);
// perform addition
int sum = decN1 + decN2;
// convert sum in base b
return getValueInBase(sum, b);
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int b = scn.nextInt();
int n1 = scn.nextInt();
int n2 = scn.nextInt();
int d = getSum(b, n1, n2);
System.out.println(d);
}
// Function to Convert any base to decimal no system
public static int getValueInDecimal(int n, int b){
int i= 0, result= 0;
while(n!=0){
result += (n % 10) * (int) Math.pow(b, i);
n /= 10; i++;
}
return result;
}
// Function to Convert Decimal to any Base number
public static int getValueInBase(int n, int b){
int convert_num = 0, pos = 0;
while(n>0){
convert_num += (n % b) * Math.pow(10,pos);
n /= b; pos++;
}
return convert_num;
}
}