codescracker


c

C Program to Print Star Pyramid Patterns



« Previous Program Next Program »


Print Patterns in C

To print patterns of numbers and stars in C programming, you have to use two for loops. The outer for loop and the inner for loop. The outer for loop is responsible for rows and the inner for loop is responsible for columns.

C Programming Code to Prints Patterns

Following are one by one C programs to print different-different patterns :

C Program - Pattern 1

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j;
	for(i=0; i<5; i++)
	{
		for(j=0; j<=i; j++)
		{
			printf("* ");
		}
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program print patterns

C Program - Patter 2

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<k; j++)
		{
			printf("* ");
		}
		k=k+2;
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

print pattern in c programming

C Program - Pattern 3

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k=8;
	for(i=0; i<5; i++)
	{
		for(j=0; j<k; j++)
		{
			printf(" ");
		}
		k=k-2;
		for(j=0; j<=i; j++)
		{
			printf("* ");
		}
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program print star pattern

C Program - Pattern 4

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, k=16, tim=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<k; j++)
		{
			printf(" ");
		}
		k=k-4;
		for(j=0; j<tim; j++)
		{
			printf("* ");
		}
		tim=tim+2;
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following output:

print star pattern in c programming

C Program - Pattern 5

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, n=1;
	for(i=0; i<5; i++)
	{
		for(j=0; j<=i; j++)
		{
			printf("%d ",n);
			n++;
		}
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

looping in c programming

C Program - Pattern 6

#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int i, j, num=1;
	for(i=0; i<5; i++)
	{
		num=1;
		for(j=0; j<=i; j++)
		{
			printf("%d ",num);
			num++;
		}
		printf("\n");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program print pattern using looping

C Program - Patter 7

Following c program ask to the user to enter the number of rows to print the pyramid of stars:

#include <stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int i, space, rows, k=0;
    printf("Enter the number of rows : ");
    scanf("%d",&rows);
    for(i=1; i<=rows; i++)
    {
	for(space=1; space<=(rows-i); space++)
	{
	   printf("  ");
	}
	while(k!=(2*i-1))
	{
	   printf("* ");
	   k++;
	}
	k=0;
	printf("\n");
    }
    getch();
}

When the above c program is compile and executed, it will produce the following result:

c program print star pyramid

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test