Java Program to print the map of India
Write a Java program to print the map of India.
Following is an obfuscated version in Java that generates the map of India.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { // Java program to print the map of India public static void main(String[] args) { int a, b, c; String S = "Greetings from Techie Delight!!" + "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQ" + "AIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r" + "\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!"; for (b = c = 10; (b + 21 < S.length()) && (a = S.charAt(b+++21))!=0;) for (; a-- > 64;) System.out.print((char)(++c == 'Z' ? c = c / 9 : 33 ^ b & 1)); } } |
Output:

Explanation:
Basically, the string is a run-length encoding of the map of India. Alternating characters in the string stores how many times to draw space and how many times to draw an exclamation mark consecutively. Here’s an analysis of the different elements of this program –
The encoded string:
"Greetings from Techie Delight!!TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJ
q TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcE
Aelclcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!"
Notice [b+++21] at the end of the encoded string. As b+++21 is equivalent to (b++ + 21) which will evaluate to 31 (10 + 21), the first 31 characters of this string are ignored and does not contribute to anything. The remaining encoded string contains instructions for drawing the map. The individual characters determine how many spaces or exclamation marks to draw consecutively.
Outer for-loop:
This loop goes over the characters in the string. Each iteration increases the value of b by one and assigns the next character in the string to a.
Inner for-loop:
This loop draws individual characters and a newline whenever it reaches the end of the line. Consider the following sysout statement.
System.out.print((char)(++c == 'Z' ? c = c / 9 : 33 ^ b & 1))
As ‘Z’ represents number 90 in ASCII, 90/9 will give us 10, a newline character. Decimal 33 is ASCII for ‘!’. Toggling the low-order bit of 33 gives you 32, which is ASCII for space. This causes ! to be printed if b is odd and a blank space to be printed if b is even.
Following is a less obfuscated version of the above code –
|
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 |
class Main { // Java program to print the map of India public static void main(String[] args) { int a = 10, b = 0, c = 10; // The encoded string after removing the first 31 characters String S = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS" + "_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelcl" + "cn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!"; // individual characters of `S` determine how many spaces or // exclamation marks to draw consecutively while (a != 0 && b < S.length()) { // read each character from the encoded string a = S.charAt(b++); while (a-- > 64) { // 'Z' is 90 in ASCII if (++c == 90) { // reset `c` to 10 ('\n' in ASCII) when // the end-of-line is reached c = 10; // print new line System.out.print((char)c); } else { // draw appropriate char depending on `b` is even or odd if (b % 2 == 0) { System.out.print('!'); } else { System.out.print(' '); } } } } } } |
That’s all about printing the map of India in Java.
Also See:
Reference: https://stackoverflow.com/questions/3533348/how-does-this-code-generate-the-map-of-india
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)