Please check my code its working on my IDE
My code is running on my IDE (eclipse) and not on this website
package lecture2; class ans1 { static void drawPattern(int N) { int n = N; int row = 1; int nst = 1; int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { int csp1 = 1; while (csp1 <= nsp1) { System.out.print(" "); csp1 = csp1 + 1; } int cst1 = 1; while (cst1 <= nst) { System.out.print(val1 + " “); val1 = val1 - 1; cst1 = cst1 + 1; } int csp2 = 1; while (csp2 <= nsp2) { System.out.print(” "); csp2 = csp2 + 1; } if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { System.out.print(val2 + " "); val2 = val2 + 1; cst2 = cst2 + 1; } } System.out.println(); if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } public static void main(String args[]) { int N = 7; drawPattern(N); } }
@discobot Whenever you use Coding Blocks IDE your class name should be “Main” And there should be any package leture 2; in the code. Also you have to take input from the user don’t give hardcoded values to N instead use scanner to scan that value from user. So corrected Code is :
class Main {
static void drawPattern(int N) {
int n = N;
int row = 1;
int nst = 1;
int nsp1 = n - 1;
int nsp2 = -1;
int val1 = row;
int val2 = 1;
while (row <= n) {
int csp1 = 1;
while (csp1 <= nsp1) {
System.out.print(" ");
csp1 = csp1 + 1;
}
int cst1 = 1;
while (cst1 <= nst) {
System.out.print(val1 + " ");
val1 = val1 - 1;
cst1 = cst1 + 1;
}
int csp2 = 1;
while (csp2 <= nsp2) {
System.out.print(" ");
csp2 = csp2 + 1;
}
if (row != 1 && row != n) {
int cst2 = 1;
while (cst2 <= nst) {
System.out.print(val2 + " ");
val2 = val2 + 1;
cst2 = cst2 + 1;
}
}
System.out.println();
if (row <= n / 2) {
nst = nst + 1;
nsp1 = nsp1 - 2;
nsp2 = nsp2 + 2;
val1 = row + 1;
val2 = 1;
} else {
nst = nst - 1;
nsp1 = nsp1 + 2;
nsp2 = nsp2 - 2;
val1 = n - row;
val2 = 1;
}
row = row + 1;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
drawPattern(N);
}
}
Hi! To find out what I can do, say @discobot display help
.
sir i’ve updated my code. it isn’t working too
@discobot There are changes that has to be made in your logic
when you re printing spaces only then instead of single space print double space. (Not in System.out.print(val1+"") or val2 syso). Corrected Code is below :
import java.util.*;
class Main {
static void drawPattern(int N) {
int n = N;
int row = 1;
int nst = 1;
int nsp1 = n - 1;
int nsp2 = -1;
int val1 = row;
int val2 = 1;
while (row <= n) {
int csp1 = 1;
while (csp1 <= nsp1) {
System.out.print(" ");
csp1 = csp1 + 1;
}
int cst1 = 1;
while (cst1 <= nst) {
System.out.print(val1 + " ");
val1 = val1 - 1;
cst1 = cst1 + 1;
}
int csp2 = 1;
while (csp2 <= nsp2) {
System.out.print(" ");
csp2 = csp2 + 1;
}
if (row != 1 && row != n) {
int cst2 = 1;
while (cst2 <= nst) {
System.out.print(val2 + " ");
val2 = val2 + 1;
cst2 = cst2 + 1;
}
}
System.out.println();
if (row <= n / 2) {
nst = nst + 1;
nsp1 = nsp1 - 2;
nsp2 = nsp2 + 2;
val1 = row + 1;
val2 = 1;
} else {
nst = nst - 1;
nsp1 = nsp1 + 2;
nsp2 = nsp2 - 2;
val1 = n - row;
val2 = 1;
}
row = row + 1;
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
drawPattern(N);
}
}
Hi! To find out what I can do, say @discobot display help
.