how to write a cprogram yo get output in the form
*
***
*****
*******
*********
*******
*****
***
*

Answer Posted / c.p.senthil

Generic solution:

n = 5
loop 1: i = 0 to 4
loop 2: i = 5 to 0

Loop variables => i j k j exp k exp
* 0 5 spaces, 1 stars (5-0) (2*0)+1
*** 1 4 spaces, 3 stars (5-1) (2*1)+1
***** 2 3 spaces, 5 stars (5-2) (2*2)+1
******* 3 2 spaces, 7 stars (5-3) (2*3)+1
********* 4 1 spaces, 9 stars (5-4) (2*4)+1
*********** 5 0 spaces, 11 stars(5-5) (2*5)+1
********* 4 1 spaces, 9 stars (5-4) (2*4)+1
******* 3 2 spaces, 7 stars (5-3) (2*3)+1
***** 2 3 spaces, 5 stars (5-2) (2*2)+1
*** 1 1 spaces, 3 stars (5-1) (2*1)+1
* 0 5 spaces, 1 stars (5-0) (2*0)+1

generalising expressions => (n-i) (2*i)+1

void printPattern(int n)
{
int i, j, k;

for(i=0; i<n; i++)
{
for(j=0; j<=(n-i); j++)
printf(" ");

for(k=0; k<(2*i+1); k++)
printf("*");

printf("\n");
}

for(i=n; i>=0; i--)
{
for(j=0; j<=(n-i); j++)
printf(" ");

for(k=0; k<(2*i+1); k++)
printf("*");

printf("\n");
}
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

I have seen function declarations that look like this

598


How do c compilers work?

602


The number of measuring units from an arbitarary starting point in a record,area,or control block to some other point a) recording pointer b) offset c) branching d) none

704


Is it possible to pass an entire structure to functions?

550


Is c easy to learn?

552






Do you know pointer in c?

584


Suggesting that there can be 62 seconds in a minute?

594


What is a nested formula?

599


Is it possible to execute code even after the program exits the main() function?

807


my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?

1113


What does a function declared as pascal do differently?

603


What are the advantages of union?

624


What is a dynamic array in c?

589


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

620


Explain what is the difference between the expression '++a' and 'a++'?

623