Friday, July 27, 2007

A pinch of C code and A cup of coffee

Last week I got a mail from one of my friend Radha Krishna who is working in Satyam. He needs some code which prints the following output.


1
2 2
3 3 3

He was asking the code in NATURAL language which no one knows. I found a P.S in his mail saying that "If u cud give me the C code for this logic I can write the equivalent NATURAL language". I have C in my system here , when I started coding I thought this is really a difficult logic to execute. Then I spent nearly 20 minutes with pen and paper with different combinations of FOR loops and successsfuly got the required sequence.

scanf("%d",&n);
k=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=k;j++)
{
printf(" ");
}
k--;
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}

4 comments:

kamakshi said...

Hi vivek,
u posted a good event with suitable epitome,very happy to see this in the blog.I know how much u tried and get the code executed in minutes.Damn cute to see your happiness and the pride in your face after succesful execution of that program.

Vivekanandan said...

Thank U

Debashis said...

great work man...
u had always been and will always b our tech genious coder....

Sudeender said...

Hey vivek,i got a question with same logic but i have to design a diamond shape in my campus interview..

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1