Pattern Generator in C - NESTED CODE || TECH FLOAT

Post Top Ad

Post Top Ad

Wednesday, 5 September 2012

Pattern Generator in C


Pattern generator in c :: 
       
     1. Write a c code to print the pattern


*

*             *
*             *             *
*             *             *             *

Ans

#include<stdio.h>
int main()
{
    int i,j,k,l;
    for(i=1;i<=4;i++)
    {
                     for(j=0;j<=i-1;j++)
                     {
                                        printf("* ");
                                        }
                                        
                                       
    printf("\n");
    }
    getch();
    return 0;
}
     
     2. Write a c code to print the pattern

       *

                *             *

*             *              *

     *                 *             *              *
Ans.
#include<stdio.h>
int main()
{
    int i,j,k,l;
    for(i=1;i<=4;i++)
    {
                     for(k=3;k>=i;k--)
                     {
                                        printf(" ");
                                        }
                     for(j=0;j<=i-1;j++)
                     {
                                        printf("*");
                                        }
                                       
                                       
    printf("\n");
    }

    getch();
    return 0;
}
   
  3.Write a c code to print the pattern
       
            *

                                *                             *
                *                             *                             *
*                             *                             *                             *

Ans.
#include<stdio.h>
int main()
{
    int i,j,k,l;
    for(i=1;i<=4;i++)
    {
                     for(k=3;k>=i;k--)
                     {
                                        printf(" ");
                                        }
                     for(j=0;j<=i-1;j++)
                     {
                                        printf("* ");
                                        }
    printf("\n");
    }
    getch();
    return 0;
}
   

5 comments:

  1. blogger_logo_round_35

    in the first progrm what is the need to take the 4rth variable?

    ReplyDelete
    Replies
    1. logo_nestedcode

      it's not really required. you may omit the 4th variable.

      Delete
  2. blogger_logo_round_35

    can anyone help me to write code for this

    p
    pr
    pro
    prog
    progr
    progra
    program

    ReplyDelete
  3. logo_nestedcode

    Your ans Mr. Rit.....
    int main()
    {
    char a[100]={'p','r','o','g','r','a','m'};
    int i,j;
    for(i=0;i<7;i++)
    {
    for(j=0;j<=i;j++)
    {
    printf("%c",a[j]);
    }
    printf("\n");
    }
    getch();
    return 0;
    }

    Try this. all the best.

    ReplyDelete
  4. logo_nestedcode

    Here is another answer Rit : u can try it out with any word...

    #include
    #include

    main()
    {
    char string[100];
    int c, k, length;

    printf("Enter a string\n");
    gets(string);

    length = strlen(string);

    for ( c = 0 ; c < length ; c++ )
    {
    for( k = 0 ; k <= c ; k++ )
    {
    printf("%c", string[k]);
    }
    printf("\n");
    }
    getch();
    return 0;
    }


    //and let us know how it works... :)

    ReplyDelete

Post Bottom Ad