- 0
Notifications Mark All Read
- Login
- Get Prime
JAVA Program for Pyramid Star Pattern
Printing Pattern
In this program we’re going to code pyramid star pattern program .
The logic of this program is this take any number input from user and store it in variable n and then run the for loop start from 0 to i
start from j=n-i to j– and again take another for loop start from j=0 to j++ to print stars and after this take line changement statement after the end of main for loop
Algorithm:
- Enter the number as input from the user and store it in any variable.(‘n‘ in this case).
- Run a loop ‘n’ number of times to iterate through each of the rows. From i=0 to i<n. The loop should be structured as for( i=0 ; i
- Run a nested loop inside the main loop to print the spaces before the pyramid. From j=n-i to j–. The loop should be structured as for (int j=n-i; j>1; j–)
- Inside this nested loop print white space System.out.print(” “); .
- Run another nested loop inside the main loop after the previous loop to print the stars in each column of a row. From j=0 to j<=i. The loop should be structured as for (int j=0; j<=i; j++ )
- Inside this nested loop print star System.out.print(” *”); .
- 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=n-i; j>1; j--) System.out.print(" "); for (int j=0; j<=i; j++ ) System.out.print(" *"); System.out.println(); } } } This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)
public static void main(String[] args) {
System.out.println(“Pattern!”);
for(int i=1;i<5;i++) {
for(int j=1;j<8;j++) {
if((i==2 &&(j==3 || j==5))||
(i==3 &&(j==2 || j==3 || j==5 || j==6))||
(i==4 &&(j==1 || j==2 || j==3 || j==5 || j==6 || j==7))
) {
System.out.print("*");
}
else if(j==4) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
You can join our Discord Channel for the same.
public class pattern{
public static void main(String[]args){
for(int i=1; ii;j–){
system.out.print(” “);
}
for(int k=1;k<=2*i-1;K++){
System.out.print("*");{
System.out.println();
}
}
}
public class pattern4 {
public static void main(String[] args) {
int n = 4;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n-i; s++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
A simpler java code
import java.util.Scanner;
public class PyramidStarPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the number of rows: “);
int r = sc.nextInt();
for (int i = 0; i < r; i++) {
for (int j = 0; j < r-i-1; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < r-i-1; j++) {
System.out.print(" ");
}
System.out.println();
}
}
}
above was incorrect
import java.util.*;
public class Main{
public static void main(String[] args){
int n = new Scanner(System.in).nextInt();
//int m = new Scanner(System.in).nextInt();
int count = 1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i;j++) System.out.print(" ");
for(int j=1;j<=count;j++){
System.out.print("*");
}
count += 2;
// for(int j=1;j<=n-i;j++) System.out.print(" ");
System.out.println();
}
}
}
Above code is for generating different pattern hence correct one is:
public class MyClass {
public static void main(String args[]) {
int n=4;
for(int i=1;ii;j–){
System.out.print(” “);
}
for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
}
}
package date3001;
import java.util.Scanner;
public class Star02 {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
System.out.println(“Enter no”);
int n = s.nextInt();
for (int i =1;i=i;j–)
System.out.print(” “);
for (int k=1;k<=i;k++)
System.out.print(" *");
System.out.println();
}
s.close();
}
}
for(int i=1;ii;j–)
System.out.print(” “);
for(int j=1;j<=i+(i-1);j++)
System.out.print("*");
System.out.println();
According to given figure the code should be:-
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main(String[] args) {
int count=0;
for(int i=1;i<=4;i++){
for(int j=1;j<=4-i;j++){
System.out.print(" ");
}
for(int j=1;j<=i+count;j++){
System.out.print("*");
}
count=count+1;
System.out.println(" ");
}
}
}
Thank you PrepInsta for publishing my code…