×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

Pattern Printing C Programs

This section contains Pyramid Programs in C, C Programs to print Star Series, and Different Pattern Printing Programs in C Language. Pattern printing programs contains Star Pattern, Number Pattern and Character Pattern printing.

All Pyramid and Pattern Printing programs based on problems popularity and frequently asked in Interview, these programs has explanation and Output.

Pyramid, Star Series and Patterns Programs in C language

Half, Full, Incremented and Decrement Stars Series, Pyramid Pattern programs

Program - 1

/*C program to print following Pyramid:
*
**
***
****
*****
*/
   
#include<stdio.h>
   
#define MAX 5
 
int main()
{
    int i,j;
 
    for(i=0; i< MAX; i++)
    {
        for(j=0;j<=i;j++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output

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

Program - 2

/*C program to print following Pyramid:
*****
****
***
**
*
*/

#include<stdio.h>

#define MAX	5

int main()
{
	int i,j;

	for(i=MAX; i>=0; i--)
	{
		for(j=0;j<=i;j++)
		{
			printf("*");
		}
		printf("\n");
	}
    return 0;
}

Output

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

Program - 3

/*C program to print following Pyramid:
        *
       * *
      * * *
     * * * *
    * * * * *
*/

#include<stdio.h>

#define MAX	5

int main()
{
    int i,j;
    int space=4;
    /*run loop (parent loop) till number of rows*/
    for(i=0;i< MAX;i++)
    {
	    /*loop for initially space, before star printing*/
	    for(j=0;j< space;j++)
	    {
		    printf(" ");
	    }
	    for(j=0;j<=i;j++)
	    {
		    printf("* ");
	    }
		
	    printf("\n");
	    space--;	/* decrement one space after one row*/
    }
    return 0;
}

Output

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

Program - 4

/*C program to print following Pyramid:
        *
       * *
      * * *
     * * * *
    * * * * *
    * * * * *
     * * * *
      * * *
       * *
        *
*/

#include<stdio.h>

#define MAX	5

int main()
{
	int i,j;
	int space=4;
	/*run loop (parent loop) till number of rows*/
	for(i=0;i< MAX;i++)
	{
		/*loop for initially space, before star printing*/
		for(j=0;j< space;j++)
		{
			printf(" ");
		}
		for(j=0;j<=i;j++)
		{
			printf("* ");
		}
		
		printf("\n");
		space--;
	}
	/*repeat it again*/
	space=0;
	/*run loop (parent loop) till number of rows*/
	for(i=MAX;i>0;i--)
	{
		/*loop for initially space, before star printing*/
		for(j=0;j< space;j++)
		{
			printf(" ");
		}
		for(j=0;j< i;j++)
		{
			printf("* ");
		}
		
		printf("\n");
		space++;
	}
    return 0;
}

Output

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

Program - 5

/*C program to print following Pyramid:
    **********
    ****  ****
    ***    ***
    **      **
    *        *
*/

#include<stdio.h>

#define MAX	5

int main()
{
	int i,j;
	int space=0;
	/*run loop (parent loop) till number of rows*/
	for(i=MAX;i>0;i--)
	{
		/*print first set of stars*/
		for(j=0;j< i;j++)
		{
			printf("*");
		}
		for(j=0;j< space;j++)
		{
			printf(" ");
		}
		/*print second set of stars*/
		for(j=0;j< i;j++)
		{
			printf("*");
		}
		
		printf("\n");
		space+=2;
	}
    return 0;
}

Output

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

Number pyramid programs - Half, Full Incremented and Decrement Series programs

Program - 6

/*C program to print following Pyramid:
        0
       01
      010
     0101
    01010
*/

#include<stdio.h>

int main()
{
	int i,j,k;
	
	for(i=0 ; i<=4 ; i++)
	{
		for(j=4 ; j>i ; j--)
			printf(" ");

		for(k=0 ; k<=i; k++)
		{
			if(k%2==0)
				printf("0");
			else
				printf("1");
		}
		printf("\n");
	}
    
    return 0;
}

Output

        0
       01
      010
     0101
    01010

Program - 7

/*C program to print following Pyramid:
    0        0
    01      01
    010    010 
    0101  0101 
    0101001010
*/

#include<stdio.h>

int main()
{
	int i,j,k,l=8,m,n,o,p;
	for(i=0; i<=4; i++)
	{
		for(j=0; j<=i; j++)
		{		
			if(j%2==0)
				printf("0");
			else
				printf("1");
		}
		for(k=1; k<=l; k++)
		{
			printf(" ");
		}
			l = l-2;
		for(m=0; m<=i; m++)
		{
			if(m%2==0)
			printf("0");
			else
			printf("1");	
		}

		printf("\n");
	}
    return 0;
}

Output

    0        0
    01      01
    010    010 
    0101  0101 
    0101001010

Program - 8

/*C program to print following Pyramid:
    12345
    1234
    123
    12
    1
*/

#include<stdio.h>

int main()
{
	int i,j;
	for(i=5; i>=1; i--)
	{
		for(j=1; j<=i; j++)
		{
			printf("%d", j);
		}
	    printf("\n");
	}
    return 0;
}

Output

    12345
    1234
    123
    12
    1

Program - 9

/*C program to print following Pyramid:
        1
       123 
      12345
     1234567 
    123456789
*/

#include<stdio.h>

int main()
{
	int i,j,k,l=1;
	for(i=1; i<=5; i++)
	{
		for(j=4; j>=i; j--)
		{
			printf(" ");
		}
		
		for(k=1; k<=l; k++)
		{ 
			printf("%d",k);
		}            
			l = l+2;    
	    printf("\n");
	}
    return 0;
}

Output

        1
       123 
      12345
     1234567 
    123456789

Program - 10

/*C program to print following Pyramid:
    1        1
    12      21
    123    321
    1234  4321
    1234554321
*/

#include<stdio.h>

int main()
{
	int i,j,k,l,m=8,n=1;
	for(i=1; i<=5; i++)
	{
		for(j=1; j<=i; j++)
		{
			printf("%d",j);
		}            
		for(k=m; k>=1; k--)            
		{
			printf(" ");
		}
			m = m-2;
		for(l=n; l>=1; l--)
		{
			printf("%d",l);
        }
  		n = n+1;
	    printf("\n");            
       }
       
       return 0; 
}

Output

    1        1
    12      21
    123    321
    1234  4321
    1234554321

Program - 11

/*
C program to print following pyramid
123454321
 1234321
  12321
   121
    1
*/

#include <stdio.h>

int main()
{
    int i,j;
    int space=0;
    
    /*Run parent loop*/
    for(i=5; i>=1; i--)
    {
        /*Print space*/
        for(j=1; j<=space; j++)
            printf(" ");
        
        
        /*Run loop to print first part of row*/
        for(j=1; j<=i; j++)
            printf("%d",j);
        
        /*Run loop to print second part of row*/
        for(j=i-1; j>=1; j--)
            printf("%d",j);
            
        printf("\n");
        space++;
    }

    return 0;
}

Output

    123454321
     1234321
      12321
       121
        1

Characters pyramid programs

Program - 12

/*
C program to print character pyramid as given below:
A 
B C 
D E F 
G H I J 
K L M N O
*/

#include <stdio.h>

int main()
{
    int i,j;
    char ch='A';
    
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%c ",ch++);
        }
        printf("\n");
    }

    return 0;
}

Output

    A 
    B C 
    D E F 
    G H I J 
    K L M N O

Program - 13

/*
C program to print character pyramid as given below:
A 
B C 
D E F 
G H I J 
K L M N O
*/

#include <stdio.h>

int main()
{
    int i,j;
    char ch;
    
    for(i=1;i<=5;i++)
    {
        ch='A';
        for(j=1;j<=i;j++)
        {
            printf("%c ",ch++);
        }
        printf("\n");
    }

    return 0;
}

Output

    A 
    A B 
    A B C 
    A B C D 
    A B C D E 

Program - 14

/*
C program to print following character pyramid:
ABCDEDCBA
ABCD DCBA
ABC   CBA
AB     BA
A       A
*/

#include <stdio.h>

int main()
{
    int i,j;
    char CH='E';
    int space=1;
    
    /*loop for row*/
    for(i=1; i<=5; i++)
    {
        /*first part of the row*/
        for(j='A'; j<=CH; j++)
            printf("%c",j);
		
        /*remove last character of first part of first row*/
        if(i==1)
            printf("\b");
		
        /*spaces between first, second part of the row*/
        for(j=1; j<space; j++)
            printf(" ");
		
        /*second part of the row*/
        for(j=CH; j>='A'; j--)
            printf("%c",j);
        
        printf("\n");
        CH--;
        space++;
    }

    return 0;
}

Output

    ABCDEDCBA
    ABCD DCBA
    ABC   CBA
    AB     BA
    A       A

Program - 15

/*
C program to print following pyramid:
    A
   BAB
  CBABC
 DCBABCD
EDCBABCDE
*/

#include <stdio.h>

int main()
{
    int i,j;
    char CH='A';
    int space=4;
    
    /*loop for row*/
    for(i=1; i<=5; i++)
    {
        /*put space*/
        for(j=1; j<=space; j++)
            printf(" ");
        
        /*first part of the row*/
        for(j=CH; j>='A'; j--)
            printf("%c",j);
        
        /*second part of the row*/
        for(j='B'; j<=CH; j++)
            printf("%c",j);
            
        printf("\n");
        CH++;
        space--;
    }

    return 0;
}

Output

        A
       BAB
      CBABC
     DCBABCD
    EDCBABCDE

Program - 16

/*
C program to print following pyramid
1A2B3C4D5E
1A2B3C4D
1A2B3C
1A2B
1A
*/

#include <stdio.h>

int main()
{
    int i,j,k;
    
    /*Run parent loop*/
    for(i=5; i>=1; i--)
    {
        for(j=1, k='A'; j<=i; j++,k++)
        {
            printf("%d%c",j,k);
        }
            
        printf("\n");
    }

    return 0;
}

Output

    1A2B3C4D5E
    1A2B3C4D
    1A2B3C
    1A2B
    1A

Program - 17

/*
C program to print following pyramid
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
*/

#include <stdio.h>

int main()
{
    int i,j;
    int space=4;
    char CH='A';
    
    /*Run parent loop*/
    for(i=1; i<=5; i++)
    {
        /*Print space*/
        for(j=1; j<=space; j++)
            printf(" ");
        
        
        /*Run loop to print first part of row*/
        for(j=1; j<=i; j++)
            printf("%c",CH+j-1);
        
        /*Run loop to print second part of row*/
        for(j=i-1; j>=1; j--)
            printf("%c",CH+j-1);
            
        printf("\n");
        space--;
    }

    return 0;
}

Output

        A
       ABA
      ABCBA
     ABCDCBA
    ABCDEDCBA

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.