C programming code to display asterisk in a rectangle pattern

Share This Topic:

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Description: C programming code to display asterisk in a rectangle pattern where the number of columns and rows are given by n and m respectively.

Algorithm

  1. Accept the number of asterisk, n to determine the column from keyboard
  2. Accept the number of asterisk, m to determine the rows from keyboard
  3. Display asterisk only for the first and last rows and columns using for loop

Code
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,m;
printf(“Enter the number of columns.\n”);
scanf(“%d”,&m);
printf(“Enter the number of rows.\n”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
if(i==1||i==n)
{
for(int j=1;j<=m;j++)
printf(““);
}
else
{
for(int k=1;k<=m;k++)
{
if(k==1||k==m)
printf(“
“);
else
printf(” “);
}
}
printf(“\n”);
}
getch();
}

The output for n=8 and m=5 is given below
IMG_0293

Share this article:
Previous Post: C programming code to display asterisk in diagonal pattern

December 2, 2014 - In Academics, Computers

Next Post: MATLAB code to prepare database of n students for 5 subjects

December 2, 2014 - In Academics, Electronics

Related Posts

Leave a Reply

Your email address will not be published.