Language – Java/C++ | Type – Puzzle | Last date 29-Apr-2009 9:00 p.m. IST | Points 5
One of the original twister puzzles !!!
Write a program to print 1 infinitely.
Twist: Do not use for, do-While or While loops.
(Assume Infinite memory if necessary!)
Got an answer? Why don’t you leave it here.
P.S - You can leave a link to your website or blog which gets published if you give the right answer. If you make it to the leader board your link gets added there!
public class Infinite1 {
ReplyDeleteprivate void print1()
{
System.out.println("1");
print1();
}
public static void main(String[]args){
Infinite1 i1 = new Infinite1();
i1.print1();
}
}
Although Java is not a recursive language, like Prolog, you can call a method inside of himself, making it infinite and reursive.
public class Infinite {
ReplyDeletestatic int number = 1;
public static void printNumber() {
System.out.println(number);
printNumber();
}
public static void main(String args[]){
printNumber();
}
}
This program will print 1 infinitely, or at least until memory runs out. Every time printNumber() is called, 1 is printed, then the method calls itself again, repeating the process. Since there is no condition to cause the method to not call itself, it will continue to run infinitely.
class Inf
ReplyDelete{
public static void main(String []args)
{
System.out.println(1);
meth(args);
}
static void meth(String[] args)
{
main(args);
}
}
nsureshreddy@live.com