-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHexBinOctConvert.java
More file actions
75 lines (75 loc) · 2.1 KB
/
HexBinOctConvert.java
File metadata and controls
75 lines (75 loc) · 2.1 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.*;
public class HexBinOctConvert {
/**
* Function that converts the number to Octal
* @param s The resultant number
* @return Octal Representation of the resultant number
*/
public static String converttoOctal(int s) {
return (Integer.toOctalString(s));
}
/**
* Function that converts the number to Hexadecimal
* @param s The resultant number
* @return Hexadecimal Representation of the resultant number
*/
public static String converttoHex(int s) {
return (Integer.toHexString(s));
}
/**
* Function that converts the number to binary
* @param s The resultant number
* @return Binary Representation of the resultant number
*/
public static String converttoBin(int s) {
return (Integer.toBinaryString(s));
}
/**
* Function to perform required mathematical operations i.e. trigonometric or logarithmic
* @param s the resultant number
*/
public static void mathOperations(int s) {
Double d1= Double.valueOf(s);
if(s%2==0) {
System.out.println(Math.sin(Math.toRadians(d1)));
System.out.println(Math.cos(Math.toRadians(d1)));
System.out.println(Math.tan(Math.toRadians(d1)));
}
else {
System.out.println(Math.log(d1));
System.out.println(Math.log10(d1));
}
}
/**
* Function to get the resulting number from the string
* @param s The actual string
* @return the resultant number after extraction and concatenation
*/
public static int getResultingNumber(String s) {
char[] str = s.toCharArray();
String resNumber="";
for(int i=0;i<str.length;i++) {
if(Character.isDigit(str[i])) {
resNumber=resNumber + Character.toString(str[i]);
}
}
int finalNumber = Integer.parseInt(resNumber);
return finalNumber;
}
/**
* The driver function
* @param args
*/
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
sc.close();
String octal,hex,binary;
int number = getResultingNumber(s1);
octal = converttoOctal(number);
hex = converttoHex(number);
binary = converttoBin(number);
System.out.print(octal + "\n" + hex + "\n" + binary + "\n");
mathOperations(number);
}
}