Java Program to Print Mountain Sequence Star Pattern

In this article we are going to see how to print mountain sequence star program.

Example-1

When row value=4

   *      *      *     * 
 ***   ***   ***   ***
********************
Example-2:

When row value= 5

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

Now, let’s see the actual program to print it.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first outer for loop to handle the rows.
  • Take first inner for loop to handle the column and print the stars according to the condition if (c >= k1 && c <= k2).
  • Then go on printing the star symbol according to loop.

JAVA Code:

Method-1: Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,k1,k2,gap;
    //creating scanner class object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
      k1 = 3;
      k2 = 3;
      gap = row ;
    // loop for handling the  row
    for(r = 1; r <= 3; r++)
    {
       //loop for  handlling the Coloumn
       for(c = 1; c <= (5 * row); c++)
       {
          if (c > k2 && r < 3)
          {
              k2 += gap;
              k1 += gap;
          }
          // Condition to printing  star in mountain pattern
          if (c >= k1 && c <= k2)
              System.out.print("*");
          else
              System.out.print(" ");
       }
       if (r + 1 == 3)
          {
            k1 = 1;
            k2 = (5 * row);
          }
       else
          {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
         }
       System.out.print("\n");
    }
  }
}

Output :

Enter row :  5

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

Method-2: User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,k1,k2,gap;
    //creating scanner class object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering any character
    System.out.print("Enter character : ");
    char mount = s.next().charAt(0);
      k1 = 3;
      k2 = 3;
      gap = row ;
    // loop for handling the  row
    for(r = 1; r <= 3; r++)
    {
       //loop for  handlling the Coloumn
       for(c = 1; c <= (5 * row); c++)
       {
          if (c > k2 && r < 3)
          {
              k2 += gap;
              k1 += gap;
          }
          // Condition to printing  star in mountain pattern
          if (c >= k1 && c <= k2)
              System.out.print(mount);
          else
              System.out.print(" ");
       }
       if (r + 1 == 3)
          {
            k1 = 1;
            k2 = (5 * row);
          }
       else
          {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
         }
       System.out.print("\n");
    }
  }
}

Output :

Enter row :  5
Enter character : @

         @            @           @              @           @
     @@@     @@@     @@@      @@@     @@@
@@@@@@@@@@@@@@@@@@@@@@@@

C Code:

#include <stdio.h>
int main() 
{
    int row,r,c,k1,k2,gap;
    printf("Enter rows: ");
    scanf("%d", &row);
      k1 = 3;
      k2 = 3;
      gap = row ;
    for(r = 1; r <= 3; r++)
    {
       for(c = 1; c <= (5 * row); c++)
       {
          if (c > k2 && r < 3)
          {
            
k2 += gap;
              k1 += gap;
          }
          if (c >= k1 && c <= k2)
              printf("*");
          else
              printf(" ");
       }
       if (r + 1 == 3)
          {
            k1 = 1;
            k2 = (5 * row);
          }
       else
          {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
         }
       printf("\n");
    }
   return 0;
}

Output :

Enter row :  5

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

C++ Code:

#include <iostream>
using namespace std;
int main()
{
    int row,r,c,k1,k2,gap;
    cout << "Enter rows: ";
    cin>> row;
      k1 = 3;
      k2 = 3;
      gap = row ;
    for(r = 1; r <= 3; r++)
    {
       for(c = 1; c <= (5 * row); c++)
       {
          if (c > k2 && r < 3)
          {
              k2 += gap;
              k1 += gap;
          }
          if (c >= k1 && c <= k2)
              cout << "*";
          else
              cout << " ";
       }
       

if (r + 1 == 3)
          {
            k1 = 1;
            k2 = (5 * row);
          }
       else
          {
            k1 = 3;
            k2 = 3;
            k1--;
            k2++;
         }
       cout << "\n";
    }
   return 0;
}
Output :

Enter row :  5

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

Related Java Star Pattern Programs: