Write a program that prints out its own source code

Frequently a question of the programs printing out their own output appears in code interviews. The question does not really test a skill of a programmer, as much as general awareness of tricks to do that. The quine page by Gary Thompson provides many examples of such applications in various languages. In case you were wondering, a program whose output is its own source code is called a quine, something that might come of use over the next interview. The aforementioned Quine Page lists the following example for C:
char*f="char*f=%c%s%c;main()
{printf(f,34,f,34,10);}%c";
main(){printf(f,34,f,34,10);}

One language that’s missing, however, is PHP. In PHP a helpful function and __FILE__ constant allow for this quick hack:
echo file_get_contents(__FILE__);

This entry was posted in General. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments on Write a program that prints out its own source code

  1. amal
    Posted 6/4/2007 at 2:47 pm | Permalink

    4: Write a complete program that inputs a positive integer n and then prints a triangle of asterisks n lines high 2n - 1 columns wide. For example, if the input is 5 then the output should be

    *
    * * *
    * * * * *
    * * * * * * *
    * * * * * * * * *

  2. vigneshwaran.balu
    Posted 6/30/2007 at 5:03 am | Permalink

    5. Write a code to print the asteriks, according to our input it must to display ?

    if input is 3 means

    *
    **
    ***

  3. sudeep
    Posted 9/28/2007 at 12:15 am | Permalink

    please write a code and describe it line by line for the following to be print on the screen
    *
    * *
    * * *
    * * * *

  4. Shivani Aggarwal
    Posted 10/30/2007 at 12:38 am | Permalink

    Write a code to print the asteriks, according to our input it must to display ?

    if input is 5 means:
    *
    * * *
    * * * * *
    * * * * * *
    * * * * * * * *

  5. Ajaya Kumar Muduli
    Posted 11/1/2007 at 4:13 am | Permalink

    how to print
    *
    * *
    * * *
    i use unix operating system and language c++.
    #include
    using namespace std;
    int main()
    {
    int n;
    system(”clear”);
    cout>n;
    for(int i=1;i

  6. Raghu
    Posted 3/2/2008 at 12:09 pm | Permalink

    Here is the code to input a positive integer n and then prints a triangle of asterisks n lines high 2n - 1 columns wide….
    Like..
    if the input is 5 then the output should be

    *
    * * *
    * * * * *
    * * * * * * *
    * * * * * * * * *

    In C source code:

    void main()
    {
    int i,j,n;
    printf(”Enter Number: “);
    scanf(”%d”,&n);
    for(i=0;i<n;i++)
    {
    for(j=0;j<2*i-1;j++)
    printf(”* “);
    printf(”\n”);
    }
    }

  7. Raghu
    Posted 3/2/2008 at 12:15 pm | Permalink

    Write a code to print the asteriks, according to our input it must to display ?

    if input is 3 means

    *
    **
    ***
    Code in C:

    void main()
    {
    int i,j,n;
    printf(”Enter Number: “);
    scanf(”%d”,&n);
    for(i=1;i<=n;i++)
    {
    for(j=0;j<i;j++)
    printf(”* “);
    printf(”\n”);
    }
    }

  8. webber
    Posted 5/11/2008 at 3:22 am | Permalink

    I use perl

    if input is 3 means

    *
    **
    ***
    #! /usr/bin/perl -w
    # task: print asteriks, according to our input
    # it must to display

    foreach(1..$ARGV[0]){
    print “*” foreach (1..$_)
    print $/;
    }

  9. s@mb@
    Posted 12/4/2008 at 8:42 am | Permalink

    Write a complete program that inputs a positive integer n and then prints a triangle of asterisks n lines high 2n - 1 columns wide. For example, if the input is 5 then the output should be

    using perl

    #!/usr/bin/perl -w

    $in=;
    @array = (1..$in);

    while ( $element = shift @array )
    {
    $test= (2 * $element) - 1;
    print ‘*’ x $test , “\n”;
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*