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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / arka bandyopadhyay

#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,k,a=4;

k=1;
clrscr();
printf("\n");
for(i=8;i>=0;i--)
{
for(j=0;j<=8 ;j++)


{
if(i>=4)
{
if(j<a ||j>(9-a-1) )
printf(" ");
else
printf(" *");



}
else
{
if(j>(8-k)||j< k)
printf(" ");
else
printf(" *");
}
}
--a;

if(i<4)k++;
printf("\n");
}


getch();
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / c.p.senthil

description for previous answer posted again

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

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Switch (i) i=1; case 1 i++; case 2 ++i; break; case 3 --i; Output of i after executing the program

5 Answers   Mascot,


What is structure pointer in c?

0 Answers  


What are the advantages of using linked list for tree construction?

0 Answers  


What is the collection of communication lines and routers called?

0 Answers  


Explain how can I make sure that my program is the only one accessing a file?

0 Answers  






say the following declaration is correct nr not. int b=a,n=0;

4 Answers   Wipro,


I want tcs placement papers of 2004-2009 , its urgent

6 Answers   TCS, Wipro,


Can a void pointer point to a function?

0 Answers  


What is 'bus error'?

0 Answers  


C program to perform stack operation using singly linked list

3 Answers  


There are N egg baskets and the number of eggs in each basket is a known quantity. Two players take turns to remove these eggs from the baskets. On each turn, a player must remove at least one egg, and may remove any number of eggs provided they all belong to the same basket. The player picking the last egg(s) wins the game. If you are allowed to decide who is going to start first, what mathematical function would you use to decide so that you end up on the winning side?

1 Answers   Hathway,


Is main an identifier in c?

0 Answers  


Categories