Java Program to Print Forward Slash Star Pattern – Definition, Methods, Examples

How to print backslash in c: Pattern Programs often pop up in most of the Java Interview Processes. Programmers will feel it difficult to answer them at times. This is where our tutorial on Java Program to Print Forward Slash Star Pattern comes into the picture. In this article, we have explained completely how to print the forward-slash star pattern and also lists out sample programs for doing so. Check out different methods for printing forward slash star patterns using static star characters and user input characters. Try to apply this knowledge while answering related programming questions and write a code of your own.

Java Program to Print Forward Slash Star Pattern

If you want to print Forward Slash Star Patterns then you can look at the examples provided below.

Example – 1

When row value=4
      *
    *
  *
*

Example – 2

When row value=5
        *
      *
    *
  *
*

How to Print Forward Slash Star Pattern?

  • Enter total row and store it an integer variable say row.
  • Take first for loop to print all the rows.
  • Take inner loop to print the column values.
  • Print the star symbol if c==(row+1-r).

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

JAVA Code:

Method-1: Static Star Character

Java Program to Print Forward Slash Star Pattern using Static Star Character

import java.util.Scanner;
class Main 
{
    public static void main(String[] args) 
    {
        int r,c,row;
        System.out.print("Enter no of rows : ");
        // make instane of Scanner class
        Scanner sc= new Scanner(System.in);
        // Store the input from user to row
        row=sc.nextInt();
    
        // loop for no of rows
        for(r=1;r<=row;r++)
        {
          // loop for printing star symbol
          for(c=1;c<=row;c++)
          {
                // if c<= row+1-r print symbol else spaces
                if(c <= (row+1-r))
                {
                   if( c == (row+1-r) )
                      System.out.print("* ");
                   else
                      System.out.print("  ");
                }
          }
          System.out.println("");
       }
   }
}

Output:

Enter no of rows : 5
        *
      *
    *
  *
*

Method-2: User Input Character

Java Program to Print Forward Slash Star Pattern using User Input Character

import java.util.Scanner;
class Main 
{
    public static void main(String[] args) 
    {
        int r,c,row;
        System.out.print("Enter no of rows : ");
        // make instane of Scanner class
        Scanner sc= new Scanner(System.in);
        // Store the input from user to row
        row=sc.nextInt();
        // Enter random character
        System.out.print("Enter symbol : ");
        char s=sc.next().charAt(0);
    
        // loop for no of rows
        for(r=1;r<=row;r++)
        {
          // loop for printing star symbol
          for(c=1;c<=row;c++)
          {
                // if c<= row+1-r print symbol else spaces
                if(c <= (row+1-r))
                {
                   if( c == (row+1-r) )
                      System.out.print(s+" ");
                   else
                      System.out.print("  ");
                }
          }
          System.out.println("");
       }
   }
}

Output:

Enter no of rows : 5
Enter symbol : ^
    ^
   ^
  ^
 ^
^

Explanation

Let’s understand the program will detailed explanation.

Let we have taken row as 5.

Iteration-I

r=1 (passed through first for loop condition) which will execute till r<=row.

Now inner for loop with c=1 checks if, c<=(row+1-r), if the condition satisfies check if c==(row+1-r). If this condition also satisfies print 1 star otherwise print 4 spaces.

    *

Iteration-II

r=2 (passed through first for loop condition) which will execute till r<=row.

Now inner for loop with c=1,2 checks if, c<=(row+1-r), if the condition satisfies check if c==(row+1-r). If this condition also satisfies print 1 star otherwise print 3 spaces.

   *

Iteration-III

r=3 (passed through first for loop condition) which will execute till r<=row.

Now inner for loop with c=1,2,3 checks if, c<=(row+1-r), if the condition satisfies check if c==(row+1-r). If this condition also satisfies print 1 star otherwise print 2 spaces.

  *

Iteration-IV

r=4 (passed through first for loop condition) which will execute till r<=row.

Now inner for loop with c=1,2,3,4 checks if c<=(row+1-r), if the condition satisfies check if c==(row+1-r). If this condition also satisfies print 1 star otherwise print 1 space.

 *

Iteration-V

r=5 (passed through first for loop condition) which will execute till r<=row. Now inner for loop with c=1,2,3,4,5 checks if c<=(row+1-r), if the condition satisfies check if c==(row+1-r). If this condition also satisfies print 1 star otherwise print 0 spaces.

*

Now r=6, so first loop condition fails. So no more loop will be executed. So we see a pattern like this in the output.

    *
   *
  *
 *
*

C Program to Print Forward Slash Star Pattern using For Loops

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

Output:

Enter no of rows : 5
        *
      *
    *
  *
*

C++ Code to Print Forward Slash Star Pattern

#include <iostream>
using namespace std;
int main() {

   int r,c,row;
 
   cout<<("Enter no of rows : ");
   cin>>row;

   for(r=1;r<=row;r++)
   {
      for(c=1;c<=row;c++)
      {
            if(c <= (row+1-r))
            {
               if( c == (row+1-r) )
                  cout<<("* ");
               else
                  cout<<("  ");
            }
      }
      cout<<("\n");
   }
   return 0;

}
Output:

Enter no of rows : 5
        *
      *
    *
  *
*

Related Java Star Pattern Programs: