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__);
-
Job Interview Question Articles
- C# Interview Questions and Answers
- QTP Interview Questions and Answers
- C++ Interview Questions and Answers
- PHP Interview Questions and Answers
- XML Interview Questions and Answers
- JavaScript Interview Questions and Answers
- Asp.Net Interview Questions and Answers
- J2EE Interview Questions and Answers
- ABAP Interview Questions and Answers
- Perl Interview Questions and Answers
- Java Interview Questions and Answers
-
Resources
- Technology Question and Answer Website
- How to dance around the salary-expectation question
- 10 mistakes managers make during job interviews
- ID Maker
- Stupid interview questions
- How to Answer These Tricky Interview Questions
- Seven tips for writing an online profile for LinkedIn, MySpace or Facebook
- Video surveillance
- Ink cartridges
- Laptop computers
- Affordable life insurance
- Ink cartridges
-
Tutorials
-
RSS Feeds

9 Comments on Write a program that prints out its own source code
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
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
5. Write a code to print the asteriks, according to our input it must to display ?
if input is 3 means
*
**
***
please write a code and describe it line by line for the following to be print on the screen
*
* *
* * *
* * * *
Write a code to print the asteriks, according to our input it must to display ?
if input is 5 means:
*
* * *
* * * * *
* * * * * *
* * * * * * * *
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
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”);
}
}
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”);
}
}
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 $/;
}
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”;
}