A Program to Print Star Pattern in C.

C program to Print star pattern .

 

Star pattern: In C language you can print any star pattern ,here you need nested loop first loop for print star and inner loop is used for line break.

 

Let’s write a program which print Star pattern ( Right angle triangle) in C.

 

Step 1: Let’s open Turbo C and write the following code.

 

Code:

 

// Add preprocessor

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n;

 

// Clear screen

clrscr();

printf(“enter range of star:”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

for(j=1;j<=i;j++)

printf(“*”);

printf(“\n”);

}

getch();

}

 

 

Step 2: Save it and name it STARPATTERN.CPP.

 

 

Step 3: Compile it by pressing ALT+F9 to check errors in program .

 

 

Step 4: Run the program and see output.

 

 

Ouput :

 

1 output

 

 

We can also print opposite star pattern (opposite right angle triangle) by write the following code.

 

Code:

 

//Add preprocessor

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n;

 

// Clear screen

clrscr();

printf(“enter range of star:”);

scanf(“%d”,&n);

for(i=n;i>=0;i–)

{

for(j=1;j<=i;j++)

printf(“*”);

printf(“\n”);

}

getch();

}

 

Output:

 

2 output

 

Print Star pattern:

 

Code:

 

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,n;

clrscr();

printf(“enter range of stars:”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{
for(j=1;j<=n;j++)

printf(“*”);

printf(“\n”);

}

getch();

}

 

Output:

 

3 output

 

Happy Coding.

 

 

 

Tags: , , , ,

  • Above pattern is known as inverted right triangle star pattern. We can print it uisng two for loop.
    for(i = rows; i > 0; i–) {
    /* Prints one row of triangle */
    for(j = i; j > 0; j–) {
    printf(“* “);
    }
    /* move to next row */
    printf(“n”);
    }