9.30.2012

Star Structure

Q. Write a c program to print the following star structure:

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

Ans.
/*c program for print the star structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,n,r,c,sp;
 printf("Enter number of rows: ");
 scanf("%d", &num);
 printf("\n");
 n=num;
 for(r=1; r<=num; r++)
 {
  for(sp=1; sp<=r; sp++)
    printf(" ");
  for(c=1; c<=n; c++)
    printf("*");
  for(c=num-r; c>=1; c--)
    printf("*");
  n--;
  printf("\n");
 }
 for(r=2; r<=num; r++)
 {
  for(sp=num-r+1; sp>=1; sp--)
    printf(" ");
  for(c=1; c<=r; c++)
    printf("*");
  for(c=r-1; c>=1; c--)
    printf("*");
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of  specific Star Structure C program
Screen shot for specific star structure C program

3 comments:

  1. we can short this code by..
    int i,j,k;
    for(i=-3;i<=3:i++)
    {
    k=i;
    if(k=0;j--)
    {
    if(j<=k)
    printf("* ");
    else
    printf(" ");
    }
    printf("\n");
    }

    ReplyDelete
  2. //ravikant kedia
    class ravikantkedia
    {
    public static void main(String args[])
    {

    int n=21;
    int m=0;//or space
    int o=n;//fror star
    boolean b1=true;
    boolean b2=true;

    for(int i=0;i<n;i++)
    {
    for(int j=0;j<m;j++)
    {
    System.out.print(" ");
    }
    for(int k=0;k<o;k++)
    {
    System.out.print("*");
    }
    if(m==n/2) b1=false;
    if(o==1) b2=false;
    m=b1? m+1:m-1;
    o=b2? o-2:o+2;
    System.out.println();

    }


    }
    }

    ReplyDelete