Program to Print Backslash Star Pattern
Java print backslash: In this article we are going to see how to print the Backslash star pattern
Example-1 When row values=4 * * * *
Example-2 When column values=5 * * * * *
How to print backslash in java: Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Approach:
- Enter total row and store it an integer variable say
row
. - Take first for loop to print all the rows.
- Take a inner loop to print the column values.
- Then go on printing the star symbols according to the iteration.
JAVA Code:
Method-1 : Static Star Character
import java.util.Scanner; public class Main { public static void main(String[] args) { // Take user input for no fo rows System.out.print("Enter the no of rows = "); Scanner sc= new Scanner(System.in); int r,c,row; // store the input value in row row=sc.nextInt(); // loop for no of rows for(r=1; r<=row; r++) { // inner loop to print symbol for(c=1; c<=row; c++) { // if row and column have same value print symbol if(r==c) System.out.print("* "); else System.out.print(" "); } System.out.println(""); } } }
Output: Enter the no of rows = 5 * * * * *
Method-2 : User Input Character
import java.util.Scanner; public class Main { public static void main(String[] args) { int r,c,row; // Take user input for no fo rows Scanner sc= new Scanner(System.in); // store the input value in row System.out.print("Enter the no of rows : "); 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++) { // inner loop to print symbol for(c=1; c<=row; c++) { // if row and column have same value print symbol if(r==c) System.out.print(s+" "); else System.out.print(" "); } System.out.println(""); } } }
Output: Enter the no of rows : 5 Enter symbol : # # # # # #
Explanation:
Let’s understand the program will detailed explanation.
Let we have taken row as 5.
Iteration-I
Star pattern in java: r=1 (passed through first for loop condition) which will execute till r<=row
.
Now inner for loop will execute 1 time and prints a symbol (if column value and row value are same) otherwise a space, which will iterate till c<=row
.
*
Iteration-II
r=2 (passed through first for loop condition) which will execute till r<=row.
Now inner for loop will execute 2 time and prints a symbol (if column value and row value are same) otherwise a space, which will iterate till c<=row
*
Iteration-III
r=3 (passed through first for loop condition) which will execute till r<=row. Now inner for loop will execute 3 time and prints a symbol (if column value and row value are same) otherwise a space, which will iterate till c<=row.
*
Iteration-IV
r=4 (passed through first for loop condition) which will execute till r<=row. Now inner for loop will execute 4 time and prints a symbol (if column value and row value are same) otherwise a space, which will iterate till c<=row.
*
Iteration-V
r=5 (passed through first for loop condition) which will execute till r<=row. Now inner for loop will execute 5 time and prints a symbol (if column value and row value are same) otherwise a space, which will iterate till c<=row.
*
Now r=6 , so first for loop condition fails. Ans no more for loop inside that will be executed. At last we will see a pattern like this as output in the output screen.
* * * * *
C Code:
#include<stdio.h> int main() { int r,c,k,row; printf("Enter the no of rows = "); scanf("%d",&row); for(r=1; r<=row; r++) { for(c=1; c<=row; c++) { if(r==c) printf("* "); else printf(" "); } printf("\n"); } return 0; }
Output: Enter the no of rows = 5 * * * * *
C++ Code:
#include <iostream> using namespace std; int main() { int r,c,row; cout<<"Enter the no of rows = "; cin>>row; for(r=1; r<=row; r++) { for(c=1; c<=row; c++) { if(r==c) cout<<("* "); else cout<<(" "); } cout<<("\n"); } return 0; }
Output: Enter the no of rows = 5 * * * * *
Related Java Star Pattern Programs: