Hello friends...
We are back with our new post on C Programming,in which we are going to tell you coding of some important patterns and pyramids.
Codings of programs in C are very easy,you just have to understand the concept and use of several functions at their respective places where they are required in your program as per your need.
To create and to run a C program properly without any error,you should firstly learn how to find out an error occured in the program,then only you will be able to diagnose that occuring error.
         


Codings for printing patterns

Let us now start telling you coding of some programs one by one:
i).Program to print following (*) pyramid:-
             *
            ***
           *****
          *******
         *********

CODING:
#include<stdio.h>
void main()
{
int i,j,k,rows,count,temp=1;
printf("Enter the number of rows:\n");
scanf("%d",&rows);
count=rows-1;
for(i=1;i<=rows;i++)
{
for(j=1;j<=count;j++)
{
printf(" ");
}
for(k=1;k<=temp;k++)
{
printf("*");
}
count--;
temp+=2;
printf("\n");
}
}


ii).Program to print following (*) pyramid:-
        *********
         *******
          *****
           ***
            *

CODING:
#include<stdio.h>
void main()
{
int i,j,k,rows,temp,count;
printf)"Enter the number of rows:\n");
scanf("%d",&rows);
count=1;
temp=1+(rows-1)*2;
for(i=1;i<=rows;i++)
{
for(j=1;j<=count;j++)
{
printf(" ");
}
for(k=1;k<=temp;k++)
{
printf("*");
}
count++;
temp-=2;
printf("\n");
}
}

iii).Program to print following (*) diamond:-
               *
              *** 
             *****
            *******
             *****
              ***
               *

CODING:
#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter the number of rows:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j;=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
n--;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf(" ");
for(j=1;j<=2*(n-i)+1;j++)
printf("*");
printf("\n");
}
}


iv).Program to print following (*) pattern:-
          *****
         *****
        *****
       *****
      *****

CODING:
#include<stdio.h>
void main()
{
int i,j,k,rows;
printf("Enter the number of rows:\n"); 
scanf("%d",&rows);
for(i=rows;i>=1;i--)
{
for(j=1;j<=i-1;j++)
printf(" ");
for(k=1;k<=rows;k++)
printf("*");
printf("\n");
}
}

v).Program to print following (*) pattern:-
                *****
                *   *
                *   *
                *   *
                *****

CODING:
#include<stdio.h>
void main()
{
int i,j,rows;
printf("Enter the number of rows:\n");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=rows;j++)
{
if(i==1 || i==rows ||j==1 || j==rows)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}

vi).Program to print following (*) pattern:-
         *     *
          *   * 
           * *
            *
           * *
          *   *
         *     *

CODING:
#include<stdio.h>
void main()
{ 
int i,j,n,count;
printf("Enter n:\n");
scanf("%d",%n);
count=n*2-1;
for(i=1;i<=count;i++)
{
for(j=1;j<=count;j++)
{
if(j==i || j==count-i+1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}

 

Comments

  1. Very nice ...keep it up πŸ‘ŒπŸ»πŸ‘ŒπŸ»πŸ‘ŒπŸ»

    ReplyDelete

Post a Comment

Popular Posts