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); } } } }
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(" ");
}
+ 1
The spaces are not printing of first if condition
0
Ya tyvn
0
Tyvm
0
Everything working fine expect first if condition if someone input 3 I want output like this
0
30
220
1110
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();
}
}
}
0
Tysm