+ 2
How to print this in java using nested loops?
1 11 121 1331 14641
3 Antworten
+ 12
import java.util.*;
public class Program
{
public static void main(String[] args) {
int rows,coef = 1;
rows= new Scanner(System.in).nextInt();
for(int i=0; i<rows; i++)
{
for(int space=0; space < rows-i; space++)
System.out.print(" ");
for(int j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
System.out.print(" "+coef);
}
System.out.println();}}}
+ 1
there are several pascal triangle codes you can refer to here
https://code.sololearn.com/cf3o8bUdK50w/?ref=app
- 1
Why the specific nested loops requirement? Why is this code not enough:
System.out.println ("1 \n 11 \n 121 \n 1331 \n 14641");