Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
JAVA Program for Square Star Pattern
May 9, 2020
Printing Square Star Pattern
In this JAVA program we will be coding a star box pattern which will have n number of stars in it rows and n of stars in column hence it will be a n x n square star pattern . The user will input a value, that will be used for determining the number of rows and columns of the pattern, and than we will use “for loop”, for printing stars at the desired places
Prerequisite:
Basic knowledge of Java language and loops
Algorithm:
Take number of rows as input from the user (length of side of the square) and store it in any variable (‘n‘ in this case).
Run a loop ‘i‘ number of times to iterate through the rows . From i=0 to i<n. The loop should be structured as for (int i = 0; i
Run a nested loop inside the previous loop to iterate through the columns. From j=0 to j<n The loop should be structured as for (int j=0 ; j
Print ‘*’ inside the nested loop to print ‘*’s in all the columns of a row.
Move to the next line by printing a new line. System.out.println();
Code in Java:
import java.util.Scanner;
public class Pattern1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no");
int n = sc.nextInt();
for (int i = 0; i<n ; i++) {
for (int j=0 ; j<n ; j++)
System.out.print("*");
System.out.println();
}
}
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)
import java.util.Scanner;
public class onehundredquestions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i =0; i<n; i++){
for (int j = 0; j<n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 4;
for (int i = n; i>0;i–){
for (int j =0; j0;i–){
for (int j =0; j<n; j++){
System.out.print("*");}
System.out.print("\n");
}
}}
1*2*3*4*5*6*7*8″9*10*{11*12*13*15*
import java.util.Scanner;
public class onehundredquestions {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i =0; i<n; i++){
for (int j = 0; j<n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int i=0;
int n=scn.nextInt();
while(i<n){
for(int k=0;k<i;k++){
System.out.print(" ");
}
for(int j=1;j<=n;j++){
System.out.print("* ");
}
System.out.println();
i++;
}
}
}
// opening curly bracket is missing
import java.util.Scanner;
public class Pattern1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter no”);
int n = sc.nextInt();
for (int i = 0; i<n ; i++) {
for (int j=0 ; j<n ; j++){ // here
System.out.print("*");
System.out.println();
}
}
}
Thank you prepinsta for publishing my code…
code galat hai bhai print ln ek niche wale bracket mein aayega
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 4;
for (int i = n; i>0;i–){
for (int j =0; j0;i–){
for (int j =0; j<n; j++){
System.out.print("*");}
System.out.print("\n");
}
}}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print("*");
}
System.out.println();
}
}
this is write code