+ 1

Why there is no output for this code I want to make pattern like this

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

3rd May 2020, 3:07 PM
Shivam Rawal
7 Respuestas
+ 2
Hey Shivam Rawal, here is the corrected code :)) for (int i = input; i>= 0; i--) { for (int j = 0; j < i; j++) { System.out.print("* "); } System.out.println(); } In the first for loop, 'i' is deceasing every time and we are running the second loop 'i'th times, so the output looks like : **** *** ** * When the input is 4!
3rd May 2020, 3:21 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 3
I did it import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int input = sc.nextInt(); for (int i=1 ;i<=input ;++i){ for (int j =1; j <= ( input - i+1) ; ++j){ System.out.print("* "); } System.out.println(); } } }
3rd May 2020, 3:23 PM
Shivam Rawal
+ 1
i=input, for ex: 4 i<=1 is false , it shloud be i>=1
3rd May 2020, 3:11 PM
Jayakrishna 🇮🇳
+ 1
tysm I didn't noticed
3rd May 2020, 3:11 PM
Shivam Rawal
+ 1
you need to check like j<=i instead of j<=input.. So replace this for (int j =1; j <= i ; ++j) You're changing decreasing i value not input value so.
3rd May 2020, 3:17 PM
Jayakrishna 🇮🇳
0
Y it is printing ***** ***** ***** and not **** *** ** *
3rd May 2020, 3:14 PM
Shivam Rawal
- 1
for(int i=input; i>=1; i--) And all other code is correct
5th May 2020, 1:07 AM
Vikas Mishra
Vikas Mishra - avatar