0

Why this code gives no output

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

12th May 2020, 5:14 AM
Shivam Rawal
7 Respuestas
+ 5
Spaces are printed. You didn't see them because you use println which outputs newline after printing spaces. use print method instead: for(int i=1;i<=n+1;++i){ System.out.print(" "); }
12th May 2020, 6:21 AM
andriy kan
andriy kan - avatar
+ 1
The spaces are not printing of first if condition
12th May 2020, 5:49 AM
Shivam Rawal
0
Ya tyvn
12th May 2020, 5:31 AM
Shivam Rawal
0
Tyvm
12th May 2020, 5:31 AM
Shivam Rawal
0
Everything working fine expect first if condition if someone input 3 I want output like this 0 30 220 1110
12th May 2020, 5:48 AM
Shivam Rawal
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); for(int j=n;j>=1;--j){ if(j==n){ for(int i=1;i<=n+1;++i){ System.out.println(" "); } System.out.print("0"); System.out.println(); } for(int i=1;i<=j-1;++i){ System.out.print(" "); } for(int i=1;i<=1;++i){ System.out.print(j); } if(j<=n-1){ System.out.print(j); } if(j<=n-2){ System.out.print(j); } System.out.print("0"); System.out.println(); } } }
12th May 2020, 5:48 AM
Shivam Rawal
0
Tysm
12th May 2020, 7:24 AM
Shivam Rawal