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 :
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:
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:
Happy Coding.