Tuesday - 18 March, 2025 +91

Java Program to Print a Copy of Program Itself


Here is another puzzle for Java programmers. Previously I posted a puzzle related to Java. Here is the solution for the Puzzle to print output as Sun Java Programmer.

Our new puzzle is you have to print the output of a Java program which contains all the source code written by you. That means whatever code you are going to write that should be printed as output.

One more point here to note is you are not allowed to use Files concept to solve this puzzle. The exact copy of source code should be printed as output of the program.

Solution:




public class N { public static void main(String [] args) { char c=34; System.out.println(s+c+s+c+';'+'}'); }

static String s="public class N { public static void main(String [] args){ char c=34;System.out.println(s+c+s+c+';'+'}');}  static String s=";
}

Copy the above code in a notepad file and save it as N.java. Compile and run the file and see the output .

Share the code with your friends if you like it.

0 comments:

Post a Comment