C++ Program to Print X Star Pattern

Write a C++ program to print X star pattern using for loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, val, rows;
     
    cout << "Enter X Pattern Rows = ";
    cin >> rows;

    cout << "X Pattern of Stars\n"; 

    val = rows * 2 - 1;

    for(i = 1; i <= val; i++)
    {
    	for(j = 1; j <= val; j++)
		{
            if(j == i || j == val - i + 1)
            {
                cout << "*";
            }
            cout << " ";
        }
        cout << "\n";
    }		
 	return 0;
}
C++ Program to Print X Star Pattern

This C++ example prints the X pattern of a given character using a while loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, val, rows;
    char ch;

    cout << "Enter X Pattern Rows = ";
    cin >> rows;

    cout << "Enter Symbol for X Pattern = ";
    cin >> ch;

    cout << "X Pattern of Stars\n"; 

    val = rows * 2 - 1;

    for(i = 1; i <= val; i++)
    {
    	for(j = 1; j <= val; j++)
		{
            if(j == i || j == val - i + 1)
            {
                cout << ch;
            }
            cout << " ";
        }
        cout << "\n";
    }		
 	return 0;
}
Enter X Pattern Rows = 10
Enter Symbol for X Pattern = #
X Pattern of Stars
#                  # 
 #                #  
  #              #   
   #            #    
    #          #     
     #        #      
      #      #       
       #    #        
        #  #         
         #          
        #  #         
       #    #        
      #      #       
     #        #      
    #          #     
   #            #    
  #              #   
 #                #  
#                  #

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.