-
Notifications
You must be signed in to change notification settings - Fork 2
/
6.1 Binary To Character Converter.java
78 lines (62 loc) · 2.1 KB
/
6.1 Binary To Character Converter.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Prof. Decosta has designed multiple sets of the question paper of CSE310: Programming in Java subject. He has assigned a separate character code (A-Z) to each set of question paper but mentioned the paper codes using binary numbers. He needs a program which can accept the binary paper code and convert it in to character code so that he can easily identify the paper code during evaluation. If the paper code entered is not having 8 bits (0 or 1) or does not corresponds to A-Z then INVALID PAPER CODE must be printed.
Input Format
First line of input is a Binary Number representing the Paper Code
Constraints
Paper code is a 8 bit binary code
Output Format
Print the Paper Code (Character) and INVALID PAPER CODE if the input is not correct.
Sample Input 0
1000101
Sample Output 0
E
Explanation 0
1000101 represents 69 which corresponds to 'E'.
Sample Input 1
1111000
Sample Output 1
INVALID PAPER CODE
Explanation 1
1111000 is having inly 7 bits
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
//taking input as a string
String input = sc.next();
if(input.length()==7)
{
int power =0;
int decimal = 0;
for(int i=6;i>=0;i--)
{
if(input.charAt(i)=='1')
{
decimal += Math.pow(2,power);
}
else if(input.charAt(i)!='0')
{
System.out.print("INVALID PAPER CODE");
break;
}
power++;
}
if(decimal>=65 && decimal<=90)
{
char code = (char)decimal;
System.out.print(code);
}
else
{
System.out.print("INVALID PAPER CODE");
}
}
else
{
System.out.print("INVALID PAPER CODE");
}
}
}