How we can make simple shapes from for loop in c++ code



Simple Triangle Code:


#include <iostream>
#include <string>
using namespace std;
int main()
                {
        for(int i=0; i<=5; i++)
        {
                for(int j=0; j<=i; j++)
                {
                                cout<<"*";
                                                }
                                                cout<<endl;
                                }
                }

Output:

*
**
***
****
*****

Other type of Triangle Code:


#include <iostream>
#include <string>
using namespace std;
int main()
                {
        for(int i=5; i>=0; i--)
        {
                for(int j=0; j<=i; j++)
                {
                                cout<<"*";
                                                }
                                                cout<<endl;
                                }
                }

Output:


******
*****
****
***
**
*

Square Shape Code:


#include <iostream>
#include <string>
using namespace std;
int main()
                {
        for(int i=0; i<=3; i++)
        {
                for(int j=0; j<=5; j++)
                {
                                cout<<"*";
                                                }
                                                cout<<endl;
                                }
                }

Output:


******
******
******
******

Diamond Shape Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
       for(int i=0,l=0; i<=col,l<=col; i=i+2,l=l+2)
{
for(int j=row; j>=l-2; j=j-2)
{
cout<<" ";
        }
        for(int k=0; k<=i-2; k++)
        {
    cout<<"*";
}
cout<<endl;
}
for(int i=0,l=0; i<=col,l<=col; l++,i=i+2)
{
for(int k=0; k<=l; k++)
       {
    cout<<" ";
}
for(int j=row; j>=i; j--)
{
cout<<"*";
        }
   cout<<endl;
 }
}

Output:

Enter number rows : 8
Enter number of colums : 8

         *
       ***
     *****
   *******
 *********
   *******
     *****
      ***
        *

Right Triangle Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
for(int i=0; i<=col; i=i+2)
{
for(int j=row; j>=i; j=j-2)
{
cout<<" ";
   }
   for(int k=0; k<=i; k++)
   {
    cout<<"*";
}
cout<<endl;

}
Output:

Enter number rows : 8
Enter number of colums : 8
         *
       ***
     *****
   *******
 *********

Opposite Right Triangle Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
for(int i=0,l=0; i<=col,l<=col; l++,i=i+2)
{
for(int k=0; k<=l; k++)
   {
    cout<<" ";
}
for(int j=row; j>=i; j--)
{
cout<<"*";
   }
   cout<<endl;
   
}

}
Output:

Enter number rows : 8
Enter number of colums : 8
 *********
   *******
     *****
       ***
         *

Another Triangle Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
for(int i=0; i<=col; i++) 
 { 
 for(int j=row; j>=i; j--) 
 { 
 cout<<"*"; 
 } 
 cout<<endl; 
 for(int k=0; k<=i; k++) 
 { 
 cout<<" "; 
 } 
 }
 }

Output:

Enter number rows : 6
Enter number of colums : 6
*******
  ******
    *****
      ****
        ***
          **
            *




Click here for watching the video of that code






avatar
AUTHOR: Muhammad Abbas
I am Software Engineer.

0 Comments