36. Program to display the following output using nested for loop.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 36:

Write a program to display the following output using nested for loop:
    *****
    *      *
    *      *
    *      *
    *****

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    for(int i=1;i<=5;i++)
    {
        if(i==1||i==5)
        {
            for(int j=1; j<=5; j++)
                cout<<"*";  
        }
        else
        {
            cout<<"*";
            for(int k=2;k<5;k++)
                cout<<" ";
            cout<<"*";  
        }
        cout<<"\n";  
    }
    getch();
}

Output:


Let me know in the comment section if you have any question.

Previous Post:
35. Program that displays all prime numbers between 100 and 500 that are also palindrome.

Comments