Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
*
+*
+*+
*+*+
*+*+*


What I have tried:

class Program
    {
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i <= n; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    if (j==i||i+j==n)
                    {
                        Console.Write("*");
                    }
                    else { Console.Write("+"); }
                    
                }
                Console.WriteLine();

            }
            Console.ReadKey();
        }
Posted
Updated 28-Mar-19 12:42pm
v3

Look at the pattern: the output alternates each character between "*" and "+" - so add something which tells you which is next, and tell it to start with a "*".
Then break the job in two: rows and then columns.
C#
bool isStar = true;
for (int row = 0; row < numberOfRows; row ++)
   {
   for (int col = 0; col <= row; col++)
      {
      Console.Write(isStar ? '*' : '+');
      isStar = !isStar;
      }
   Console.WriteLine();
   }
 
Share this answer
 

The pattern is a repeating sequence of '*' and '+' split into lines, where the length of each line is equal to the line number, starting from line 1.The modulus operator is excellent for for iterating over a sequence. So keep a count of the characters printed, use count % 2 to alternate between the two characters then print a carrage return when the number of characters in the line is equal to the line number.


C#
static void Main(string[] args)
       {
           int count = 0;
           for (int line = 1; line <= 5; line++)
           {
               int lineEnd = count + line;
               while (count < lineEnd)
               {
                   Console.Write(count % 2 == 0 ? '*' : '+');
                   count++;
               }
               Console.WriteLine();
           }
           Console.ReadKey();
       }

A more flexible but less simple approach is to use a method that can handle any repeating pattern of any length.


C#
static void PrintPattern(string pattern,int lines)
     {
         //To do:check for valid arguments
         int count = 0;
         for (int line = 1; line <= lines; line++)
         {
             int lineEnd = count + line;
             while (count < lineEnd)
             {
                 Console.Write(pattern[count % pattern.Length]);
                 count++;
             }
             Console.WriteLine();
         }
         Console.ReadKey();
     }
 
Share this answer
 
v2
Quote:
I need a solution for this pattern in easy way.

The only easy way is having someone paid to do the job.
As programmer, your job is to create algorithms
First of all, analyze the problem:
1) the shape is a triangle
2) each line is made of alternated + and *
3) lines 1, 4 and 5 start with *, lines 2 and 3 start with +

You forgot to state a problem with your code
C++
static void Main(string[] args)
{
    int n = Convert.ToInt32(Console.ReadLine());
    int offset;
    for(int i = 0; i <= n; i++)
    {
        offset = 0;
        for (int j = 0; j <= i; j++)
        {
            if ((j + offset) % 2)
            {
                Console.Write("*");
            }
            else
            {
                Console.Write("+");
            }
        }
        Console.WriteLine();
    }
    Console.ReadKey();
}

This code will print a triangle (1) with alternating * and + (2).
you just have to find how to change offset = 0 to follow starting char pattern (3).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900