C programs to print pyramid patterns

Q. C program to print stars or numbers to make pyramid patterns.

PATTERN 1 :
print-ladder-left-numbers

SOLUTION :

#include <stdio.h>

int main()
{
	int rw, c, no=1 ,len;

	printf("\n Enter number of rows : ");
	scanf("%d",&len);

	for(rw=1; rw<=len; rw++)
	{
		printf("\n");
		for(c=1; c<=rw; c++)
		{
			printf(" %2d ", no);
			no++;
		}
	}
	printf("\n");
	return 0;
}

PATTERN 2 :
print-ladder-left-star

SOLUTION :

#include<stdio.h>

int main()
{
    int n, i, j;

    printf("\n Enter number of rows : ");
    scanf("%d",&n);

    for ( i = 1 ; i <= n ; i++ )
    {
        printf(" ");
        for( j = 1 ; j <= i ; j++ )
            printf("* ");
            printf("\n");
    }

    return 0;
}

PATTERN 3 :
print-ladder-left-star-reverse

SOLUTION :

#include<stdio.h>

int main()
{
    int n, i, j, temp;

    printf("\n Enter number of rows : ");
    scanf("%d",&n);

    temp = n;

    for ( i = 1 ; i <= n ; i++ )
    {
        printf(" ");
        for( j = 1 ; j <= temp ; j++ )
            printf("* ");
        temp--;
        printf("\n");
    }

    return 0;
}

PATTERN 4 :
print-ladder-right-star

SOLUTION :

#include<stdio.h>

int main()
{
    int n, c, k, blank;

    printf("\n Enter number of rows : ");
    scanf("%d",&n);

    blank = n;

    for ( k = 1 ; k <= n ; k++ )
    {
        printf(" ");
        for ( c = 1 ; c < blank ; c++ )
            printf(" ");

        blank--;

        for( c = 1 ; c <= k ; c++ )
            printf("*");

        printf("\n");
    }

    return 0;
}

PATTERN 5 :
print-ladder-right-star-reverse

SOLUTION :

#include<stdio.h>

int main()
{
   int n, i, j, blank;
   printf("\n Enter number of rows : ");
   scanf("%d",&n);

   blank = 0;

   for ( i = n ; i >= 1 ; i-- )
   {
       printf(" ");
       for ( j = 1 ; j <= blank ; j++ )
       printf(" ");

       blank++;

       for ( j = 1 ; j <= i ; j++)
        printf("*");
       printf("\n");
   }

   return 0;
}

PATTERN 6 :
print-ladder-string

SOLUTION :

#include<stdio.h>
#include<string.h>

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

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

      length = strlen(string);
      printf("\n");

      for ( c = 0 ; c < length ; c++ )
      {
          printf(" ");
          for( i = 0 ; i <= c ; i++ )
          {

               printf("%c", string[i]);
          }
          printf("\n");
      }

      return 0;
}

PATTERN 7 :
print-left-base-pyramid

SOLUTION :

#include<stdio.h>

int main()
{
    int n, c, k;

    printf("\n Enter number of rows : ");
    scanf("%d",&n);

    for ( c = 1 ; c <= n ; c++)
    {
        printf(" ");
        for ( k = 1 ; k <= c ; k++ )
            printf("*");

        printf("\n");
    }

    for ( c = n - 2 ; c >= 0 ; c-- )
    {
        printf(" ");
        for ( k = c ; k >= 0 ; k-- )
            printf("*");

        printf("\n");
    }

    return 0;
}

PATTERN 8 :
print-pyramid

SOLUTION :

#include<stdio.h>

int main()
{
   int row, i, n, temp;

   printf("\n Enter the number of rows in pyramid : ");
   scanf("%d",&n);

   temp = n;

   for ( row = 1 ; row <= n ; row++ )
   {
       printf(" ");
       for ( i = 1 ; i < temp ; i++ )
        printf(" ");

       temp--;
       for ( i = 1 ; i <= 2*row - 1 ; i++ )
        printf("*");
       printf("\n");
   }

   return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s