Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
Program in C++
3.2.
Program in C
3.3.
Program in Java
3.4.
Program in Python
4.
Frequently Asked Questions
4.1.
Why are Patterns essential to learn? 
4.2.
What Is A Design Pattern?
5.
Conclusion
Last Updated: Mar 27, 2024

Cross Sign Pattern

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Problem Statement

Program to Print Cross Sign (╳ ) Star Pattern.

Example

Input : n=5

Output:

*   *
 * * 
  *  
 * * 
*   *

Solution

Program in C++

The C++ Program to Print Cross Sign (╳) source code is available here. Star Pattern.

#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter the row size:";
    int row_size;
    cin>>row_size;
    int in,out,p;
     for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==out || in+out==row_size+1)
               {
               cout<<"*";
                 }
               else
               {
                 cout<<" ";
               }
             }
            cout<<"\n";
     }
}

You can also try this code with Online C++ Compiler
Run Code

Output:

Enter the row size:7
*     *
 *   *
  * *
   *
  * *
 *   *
*     *
You can also try this code with Online C Compiler
Run Code

Program in C

The C Program to Print Cross Sign (╳ ) source code is available here, Star Pattern.

#include<stdio.h>
int main()
{
    int out,in;
    printf("Enter the row size:");
int row_size;
scanf("%d",&row_size);
        for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==out || in+out==row_size+1)
               {
  printf("*");
}
else
         {
             printf(" ");
         }}
        printf("\n");
}}
You can also try this code with Online C Compiler
Run Code

Input/Output:

Enter the row size:5
*   *
 * *
  *
 * *
*   *

Program in Java

The Java Program to Print Cross Sign (╳) Star Pattern source code is available here.

import java.util.Scanner;
public class StarPattern17 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
        System.out.println("Enter the row size:");
       int out,in;
       int row_size=cs.nextInt();
       for(out=1;out<=row_size;out++)
       {
           for(in=1;in<=row_size;in++)
           {
               if(in==out || in+out==row_size+1)
               {
                  System.out.printf("*");
               }
               else
               {
                System.out.printf(" ");    
               }
           }
           System.out.println(); 
       }
       cs.close();
}
}
You can also try this code with Online Java Compiler
Run Code

Input/Output:

Enter the row size:
5
*   *
 * * 
  *  
 * * 
*   *

Program in Python

The Python Program to Print Cross Sign (╳) Star Pattern source code is available here.

row_size=int(input("Enter the row size:"))
print_control_x=row_size//2+1
for out in range(1,row_size+1):
    for inn in range(1,row_size+1):
        if inn==out or inn+out==row_size+1:
            print("*",end="")
        else:
            print(" ", end="")
    print("\r")
You can also try this code with Online Python Compiler
Run Code

Input/Output:

Enter the row size:3
* *
 * 
* *

In all the above examples, the output will be a pattern in the form of a Cross Sign.

Input Format

The first line contains the row size of the pattern, while the other variables were for the internal and external of the pattern.

Output Format

We are printing the pattern in the form of a Cross Sign.

Check out this problem - Check If A String Is Palindrome

Frequently Asked Questions

Why are Patterns essential to learn? 

Patterns help us make predictions of what will happen. Next, It helps us develop logical connections and improve our reasoning skills

What Is A Design Pattern?

A design pattern is a general repeatable solution to a typically occurring problem in software design in software engineering. A design pattern isn't a finished design that can be turned into code right away. It's a description or template for solving an issue that may be applied to a variety of scenarios.

Conclusion

In this article, we have learned the Cross Sign pattern using the stars. We also explained it works and how it will go through step by step. We also took the example and the output of the pattern.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass