Let us use C program to print following patterns.
Click on Pattern to see the C program
Sample Pattern Programs
// C program to print Square pattern based on given value of N.
#include<stdio.h>
int main()
{
int i,j,n;
printf("Enter value of n:");
scanf("%d",&n);
//Following loop prints square based on value of n...
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("* "); //1 space for proper output
printf("\n");
}
return 0;
}
Output of Program:
Enter value of n:5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
|
/* C program to print triangle as shown below:
For N=5, print output as
* * * * *
* * * *
* * *
* *
*
*/
int main()
{
int i,j,n=5;
for(j=0; j<n; j++)
{
for(i=n; i>n-j; i--)
printf(" "); for(i=0; i<n-j; i++) printf("* "); printf("\n");
}
return 0;
}
|
/* C Program to Print Patter like character H.
For N=5,
* *
* *
* *
* * * * *
* *
* *
* *
*/
#include<stdio.h>
int main()
{
int i,j,n=5;
for(j=0; j<n-2; j++)
{
printf("* ");
for(i=0; i<n-2; i++)
printf(" ");
printf("* \n");
}
for(i=0; i<n; i++)
printf("* ");
printf("\n");
for(j=0; j<n-2; j++)
{
printf("* ");
for(i=0; i<n-2; i++)
printf(" ");
printf("* \n");
}
return 0;
}
Output of the program:
* *
* *
* *
* * * * *
* *
* *
* *
|
/*C program to print Square with inner pattern.
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
*/
#include<stdio.h>
int main()
{
int i,j, n=5;
for(i=0; i<n*2; i++)
printf("* ");
printf("\n");
for(j=0; j<n-1; j++)
{
for(i=0; i<n-j-1; i++)
printf("* ");
for(i=0; i<j+1; i++)
printf(" ");
for(i=0; i<n-j-1; i++)
printf("* ");
printf("\n");
}
for(j=0; j<n-1; j++)
{
for(i=0; i<=j; i++)
printf("* ");
for(i=0; i<n-j-1; i++)
printf(" ");
for(i=0; i<=j; i++)
printf("* ");
printf("\n");
}
for(i=0; i<n*2; i++)
printf("* ");
printf("\n");
return 0;
}
|
* * * * *
thanks
ReplyDelete