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
Write a program to check prime number in c programming?
What is c mainly used for?
what are the 10 different models of writing an addition program in C language?
How can I find the modification date of a file?
What does 2n 4c mean?
Explain bitwise shift operators?
Difference between macros and inline functions? Can a function be forced as inline?
What is int main () in c?
.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }
How can you find the exact size of a data type in c?
Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted.
What is c variable?
What is getch?
Explain a pre-processor and its advantages.
What does the error message "DGROUP exceeds 64K" mean?