SHREE GANESHA PATTERN PROGRAM IN C - Coder Insect
C Program to Print Shree Ganesha using patterns
In this example you will learn to print required pattern (as show in image below code) in C programming .
Here, delay() function is used to suspend executon of program for particular time. To include delay() function in program, use #include<dos.h> header file for windows and #include<unistd.h> header file for unix/linux system.
Also, printf("%*s",<s>,"text") is used to right align text by variable number of spaces. Color can be added to program using printf() function in C. We have 8 ANSI colors (can be doubled to 16 by considering standard and bold ) .
Colors used in our program:
printf("\033[1;31m"); //output in Bold Red color
printf("\033[1;32m"); //output in Bold Green color
printf("\033[1;33m"); //output in Bold Yellow color
printf("\033[1;34m"); //output in Bold Blue color
To understand patterns programs logic, you should have basic knowledge of looping statements in C (for loop) .
C program:
//printing Ganesha using different patterns in C
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main( )
{
int i , j , k;
clrscr( );
//for pyramidal crown
for(i=0;i<5;i++)
{
printf(“%*s”,35);
for(j=i ;j<5;j++)
{
printf(“ “); //to print spaces
}
for(k=0;k<((2*i)-1);k++)
{
printf(“\033[1;32m”); //to set green color to crown
printf(“*”);
}
printf(“\n”);
delay(400); //to suspend execution for 400 milliseconds
}
//for head and ears
for(i=0;i<4;i++)
{
printf(“%*s”,31);
for(j=0; j<i; j++)
{
printf(“ “);
}
for(j=i ;j<4;j++)
{
printf(“\033[1;31m”); //to set red colour for ears
printf(“#”);
}
printf(“%*s”,1);
for(j=1;j<8; j++)
{
printf(“\033[1;33m”); //to set yellow colour for head
printf(“@”);
}
printf(“%*s”,1);
for(j=i; j<4;j++)
{
printf(“\033[1;31m”);
printf(“#”);
}
printf(“\n”);
delay(600);
}
//for mouth
for(i=0; i<2; i++)
{
printf(“%*s”,38);
for(j=0; j<4; j++)
{
printf(“\033[1;33m”);
printf(“@”);
}
printf(“\n”);
delay(400);
}
for(i=0; i<3; i++)
{
printf(“%*s”,39);
for(j=0; j<i; j++)
{
printf(“ “);
}
for(k=0; k<3; k++)
{
printf(“\033[1;33m”);
printf(“@”);
}
printf(“\n”);
delay(400);
}
for(i=0; i<2;i++)
{
printf(“%*s”,42);
for(j=0; j<i ;j++)
{
printf(“ “);
}
for(k=0; k<2; k++)
{
printf(“\033[1;33m”);
printf(“@”);
}
printf(“\n”);
delay(400);
}
printf(“%*s”,45);
printf(“\033[1;33m”);
printf(“@”);
delay(400);
getch( ); //execution stops
}
Output:
(In image format)
Output:
( In video format to show delay() function)
Well , that's it ! Just try this by your own.
I'll be happy to answer questions in the comments .
Stay safe..... keep codingπ.
Fabπ€©
ReplyDeleteNiceπ
ReplyDeleteGreat ππππ
ReplyDeleteIt's amazing!!
ReplyDeletekeep it upπ
Good π
ReplyDeleteGood ππ
ReplyDelete