1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

star pattern

Discussion in 'C' started by mrunalini, Nov 20, 2009.

  1. mrunalini

    mrunalini New Member

    Joined:
    Aug 15, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Q: Genrate star pattern below
    Code:
      *               *
      *   *       *   *
      *   *   *   *   *
    
    Program:
    Code:
    main()                                                                        
    {                                                                             
            int a=3,i,j;                                                    
            char k='*';                                                   
            clrscr();                                             
            for(i=1;i<=3;i++)                                                     
            {                                                                     
                    for(j=1;j<=i;j++ )                                            
                    printf("%3c",k);                                              
                    for(j=1;j<=a;j++)                                             
                    printf("   ");                                                
                    a-=2;                                                        
                    for(j=1;j<=i;j++)                                             
                    printf("%3c",k);                                              
                    printf("\n");                                               
            }
    getch();      
    }       
    
    O/P:
    Code:
      *                 *
      *  *         *  *
      * *   *    *  *  *
    
    This is the o/p i m getting what should be done to remove this one extra star in last line.
    if i put "j<=2" in last for loop check condition then this extra star moves in first row like this

    O/P:
    Code:
      *                 *  *
      *  *         *  *
      *  *   *    *  *  
    
    so plz suggest right solution to get desired o/p.
     
    Last edited by a moderator: Nov 21, 2009
  2. mrunalini

    mrunalini New Member

    Joined:
    Aug 15, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    star pattern1

    Q: Genrate star pattern below like crown
    total 3 rows ,
    and 5 columns,
    5stars in 3rd row ,
    2stars on right and 2stars on left side i.e total 4 stars in 2nd row,
    1star on right and 1 star on left side i.e. total 2 stars in 1 st row,
    * *
    * * * *
    * * * * *
    __________________________________________________
    Program:
    Coding:
    main()
    {
    int a=3,i,j;
    char k='*';
    clrscr();
    for(i=1;i<=3;i++)
    {
    for(j=1;j<=i;j++ )
    printf("%3c",k);
    for(j=1;j<=a;j++)
    printf(" ");
    a-=2;
    for(j=1;j<=i;j++)
    printf("%3c",k);
    printf("\n");
    }
    getch();
    }
    O/P:
    * *
    * * * *
    * * * * * * This is the o/p i m getting what should be done to remove this one extra star in last line.
    if i put "j<=2" in last for loop check condition then this extra star moves in first row like this
    O/P:
    * * *
    * * * *
    * * * * *
    so plz suggest right solution to get desired o/p.




    [/quote]
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Code Blocks you should use.
     

Share This Page