0

How do I do this? 1 1. 2. 1 1. 2. 3. 2. 1 1. 2. 3. 4. 3. 2. 1

12th Aug 2016, 8:55 AM
M.Mirajus Salehin
M.Mirajus Salehin - avatar
5 Answers
+ 3
import java.util.Arrays; public class Program { public static void main(String[] args) { int r = 4; char[] chars = new char[r]; Arrays.fill(chars, ' '); String spaces = new String(chars); for (int i=0; i < r; i++){ // print space System.out.print(spaces.substring(i)); // print left number for(int j=1; j<=i+1; j++){ System.out.printf("%2d",j); } // print right number for(int j=i; j > 0; j--){ System.out.printf("%2d",j); } // print new line System.out.println(); } } }
12th Aug 2016, 2:24 PM
WPimpong
WPimpong - avatar
+ 1
how can I do this?
12th Aug 2016, 9:32 AM
Machava Agostinho
Machava Agostinho - avatar
0
If you understand this C code, you can do it with Java. This triangle called Pascal triangle. #include <stdio.h> long fact(int); int main() { int line, i, j; printf("Enter the no. of lines: "); scanf("%d", &line); for (i = 0; i < line; i++) { for (j = 0; j < line - i - 1; j++){ printf(" "); } for (j = 0; j <= i; j++){ printf("%ld ", fact(i) / (fact(j) * fact(i - j))); } printf("\n"); } return 0; } long fact(int num) { long f = 1; int i = 1; while (i <= num) { f = f * i; i++; } return f; } OUTPUT Enter the no. of lines: 4 1 1 1 1 2 1 1 3 3 1
12th Aug 2016, 10:19 AM
Lord Centillion
Lord Centillion - avatar
0
thanks for the suggestion
12th Aug 2016, 11:50 AM
M.Mirajus Salehin
M.Mirajus Salehin - avatar
0
What in the question is not pascal triagle.
12th Aug 2016, 1:14 PM
WPimpong
WPimpong - avatar