Pattern Numbers & Stars 1

//what is lacking in my logic it is correct for all input then why its showing wrong for just1 1 input and passed other inputs.
//
import java.util.;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2
n-2; j++) {
if(j<=n+1-i)
System.out.print(j+" ");

		   if(j<=n-2+i && j>=n+2-i)
			   System.out.print("*"+" ");
	 
		}
		System.out.println();
	}
}

}

Hi Nirmal
Your code doesn’t work for the case when the input is 1. For n = 1, output should be 1. Just change the condition in the inner for loop to j <= 2n - 1.

import java.util.
;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2*n-1; j++) {
if(j<=n+1-i)
System.out.print(j+" ");

	   if(j<=n-2+i && j>=n+2-i)
		   System.out.print("*"+" ");
 
	}
	System.out.println();
}

}
}

Hey Nirmal, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.