The Shortest (again!) [Problem of the Week]

Once that a blogger says “i am too busy to post and i will start posting regularly once i am free” he never actually becomes free and never it happens that you see a post in his blog again..

so i wont say that .. but since i am busy a bit .. i will leave you with a strange problem this time.. famous among hackers.. its about..

The Shortest Program That Can Print It Self

..ignoring header files..

so lets say you write this program

#include <stdio.h>void main(){}

your program must print to the console this

void main(){} press any key to exit..
and off course .. any other letter you add to the code (e.g. the printing code inside the main) must be printed also along with the code
Rules and Hints:
  1. the code may be in C or C++
  2. you may consider using either printf or cout to print the program any thing is fine, printf is recommended.
  3. if you can also print the #include lines.. (honestly I couldn’t).. then you are the man.
  4. “almost right” answers might be accepted as long as it RUNS and at least prints 95% of the program!
  5. Anonymous answers wont be approved, at least provide a nick name and don’t use m8 cause its mine!
  6. if you still don’t get the problem, try writing a program that prints “int main(){}” and then look at your code .. and then try to add what you added to the printed “int main(){}” and you will get the problem.
    in other words .. i need to copy the console output and place it in a cpp and run it and then take its output place it in a cpp and .. bla bla

22 thoughts on “The Shortest (again!) [Problem of the Week]

  1. We have a first hacker

    First Place

    Mohamed Samy

    and he included the #include line in the printing (Y) πŸ˜€

    guys you can use this site “pointed by Mohamed Samy” to get a link for your code, because wordpress destroys the formatting

  2. To Mohamed Abd El-Mon’em (Harry Potter)
    replace while(str) cout..*str++;
    with cout..str; only .. it will work then you wont need the tmp var
    try reading about Printf because cout is not that cool
    if u use printf you will be able to use the same trick you used with the other characters πŸ˜‰

  3. To Abdalla (Jaqoup)
    well, that was new πŸ˜€ .. very good solution in deed πŸ˜€ .. i would have never thought about it that way πŸ˜€

    First Place

    Mohamed Samy
    Mohamed Abdelghani

  4. Thanx Fouad
    I got it with cout, I thought in printf but I found the same problem
    (printf(“%c”,char(34));) the double quotations here also , sure their is
    a solution with it but I can’t got it.

  5. To Mohamed Abd El-Mon’em (Harry Potter)
    Man, that is very very good (Y)

    First Place

    Mohamed Samy
    Mohamed Abdelghani
    Mohamed Abd El-Mon’em (Harry Potter)

  6. mesh yenfa3 deh ??
    #define STR(str) cout<<#str

    we a7ot kol el code fe STR ……ana faker eny shoft el so2al dah fe site 2abl keda bas mesh kont fehemt el 7al

  7. #include
    using namespace std;

    #define STR(str)
    cout <<“int main(){n” << #str<<“n}”;
    int main()
    cout << “this is koko program”;
    return 0;)

  8. Question:
    should the output be formatted the same as i wrote in the code ?
    i mean the new lines and so on ..

  9. To Tasniem
    if you can .. then it would be great .. but we can always write the code in one line πŸ˜€ so it matches the output .. can’t we ? πŸ˜€
    waiting for your solution ..

  10. we have other three winners πŸ˜€
    AlaaShaker (wait till you see his solution) πŸ˜€
    Hatem Abdelghani
    and a 97% right solution form Tecnoyotta which actually is the first to use this method πŸ˜€

    thanks guys

Leave a Reply

Your email address will not be published. Required fields are marked *