0% found this document useful (0 votes)
75 views

Practical No:-AIM: - Write A Program To Print Different " " Patterns - 1. Source Code

The document contains code for printing different star patterns including a triangle, diagonal, diamond, and triangle with spaces pattern. Each code sample includes header files, takes user input for number of rows, contains nested for loops to print spaces and stars, and outputs the pattern. The document provides the source code and output for programs to print 4 different star patterns with varying logic in the nested for loops to generate the shapes.

Uploaded by

saliniprabha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Practical No:-AIM: - Write A Program To Print Different " " Patterns - 1. Source Code

The document contains code for printing different star patterns including a triangle, diagonal, diamond, and triangle with spaces pattern. Each code sample includes header files, takes user input for number of rows, contains nested for loops to print spaces and stars, and outputs the pattern. The document provides the source code and output for programs to print 4 different star patterns with varying logic in the nested for loops to generate the shapes.

Uploaded by

saliniprabha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL NO:AIM:- Write a Program to print different * Patterns .

1.
SOURCE CODE://Program to Print Triangle pattern of Stars
#include<stdio.h>
#include<conio.h>
int main( )
{
clrscr( );
int i, j, k, n;
printf("Enter no. of rows to be printed\t");
scanf("%d",&n);
for (i=0; i<=n; i++)
{ for (j=n-i;j>=1;j--)
{
printf(" ");
}
for (k=1; k<=i; k++)
{
printf("* ");
}
printf("\n");
}
getch( );
}

OUTPUT:-

//for clrscr( )

//Loop to print no. of Rows


//Loop to print spaces..

//Loop to print Star patterns.

2.
SOURCE CODE://Program to Print Diagonal pattern of Stars
#include<stdio.h>
#include<conio.h>
//for clrscr( )
int main( )
{
clrscr( );
int n, temp, i, j;
printf("Enter the no. of rows to be printed\t");
scanf("%d",&n);
temp=n;
for (i=0; i<n; i++)
//For Rows to be printed.
{
for (j=1; j<temp; j++)
//For Spaces Before Star.
printf(" ");
temp--;
printf("*");
printf("\n");
}
return 0;
getch( );
}

OUTPUT:-

3.
SOURCE CODE://Program to Print Diamond pattern of Stars
#include<stdio.h>
#include<conio.h>
int main( )
{
int i, j, k, n, n1, n2;
printf("Enter no. of rows to be printed\t");
scanf("%d",&n);
n1=(n+1)/2;
//Loop to Print Upper half of the Diamond..
for (i=0; i<=n1; i++)
{
for (j=n1-i; j>=1; j--)
{
printf(" ");
}
for (k=1; k<=i; k++)
{
printf("* ");
}
printf("\n");
}
n2=n1-1;
//Loop to print Lower Half of Diamond..
for (i=n2; i>=0; i--)
{
for (j=(n1-i); j>=1; j--)
{
printf(" ");
}
for (k=i; k>=1; k--)
{
printf("* ");
}
printf("\n");
}
getch( );
}

OUTPUT:-

//for clrscr( )

//Loop to print no. of rows.


//Loop to print no. of Spaces.

//Loop to print Star Pattern.

4.

SOURCE CODE://Program to Print Triangle pattern of Stars space in between


#include<stdio.h>
//Header file.
#include<conio.h>
//For clrscr( )..
int main( )
{
clrscr( );
int n, temp, i, j;
printf("Enter the no. of rows to be printed\t");
scanf("%d",&n);
temp=n;
for (i=0; i<n; i++)
//For no of Rows
{
for (j=1; j<temp; j++)
//For Spaces before Star
printf(" ");
temp--;
printf("*");
for (j=1; j<=(2*i-1); j++)
//For Making Triangle By Star.
{
if(i==(n-1))
printf("*");
else
printf(" ");
}
if(i>0)
printf("*");
printf("\n");
}
return 0;
}

OUTPUT:-

You might also like