top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to print a rectangle using loops (for/while) in C?

+2 votes
751 views
How to print a rectangle using loops (for/while) in C?
posted Jan 28, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
#include<stdio.h>

void rectangle(int m,int n)
{
    int i,j;

    for(i=0;i<n;i++)
    {
        if(i==0)
        {
            for(j=0;j<m;j++)
            {
                printf("*");
            }
        }
        if(i==n-1)
        {
            for(j=0;j<m;j++)
            printf("*");
        }
        if(i!=0&&i!=n-1)
        {
            printf("*");
            for(j=1;j<m-1;j++)
                printf(" ");
            printf("*");
        }
        printf("\n");
    }
}

int main()
{
    int m,n;
    printf("ENTER THE LENGTH AND WIDTH OF RACTANGLE: ");
    scanf("%d%d",&m,&n);
    rectangle(m,n);
}
answer Dec 20, 2015 by Shishir Chaudhary
0 votes
#include <stdio.h>

int main(void)
{
    int length=5, width=4, x;

    for (x = 1; x <= width; x++)
    {
        printf("*");
    }

    for (x = 0; x <= length - 2; x++)
    {
        x = 0;

        for (; x == 0; x++)
        {
            printf("*");
        }

        for (; x <= width - 2; x++)
        {
            printf(" ");
        }

        for (; x == width; )
        {
            printf("*\n");             
        }
    }

    for (x = 1; x <= width; x++)
    {
            printf("*");
    }
    return 0;
}
answer Jan 28, 2015 by Salil Agrawal
Similar Questions
+10 votes

Can you print spiral number patterns using only loops (for/while), conditions (if/else) and basic maths? i.e. *without the use of libraries/data structures/arrays*

sample input :4
sample output:

1    2   3  4
12  13  14  5
11  16  15  6
10   9   8  7
...