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

What is a method in c?

630


Study the following C program :call_me (myvar)int myvar;{ myvar +- 5; }main(){int myvar;myvar = 3;call_me(myvar);printf("%d ",myvar);What will be printed a) 3 b) 5 c) 8 d) symbol

670


What is default value of global variable in c?

564


What is hungarian notation? Is it worthwhile?

703


what are non standard function in c

1439






What are run-time errors?

602


What are pointers? What are stacks and queues?

585


Design a program which assigns values to the array temperature. The program should then display the array with appropriate column and row headings.

1770


What is the maximum no. of arguments that can be given in a command line in C.?

674


How does normalization of huge pointer works?

644


Write a program to compute the similarity between two strings - The program should get the two strings as input - Then it will output one single number which is the percentage of similarity between the two strings

2253


How do I send escape sequences to control a terminal or other device?

615


Explain the properties of union.

615


What are actual arguments?

649


What is the purpose of void pointer?

603