pascal triangle c 2b 2b

Solutions on MaxInterview for pascal triangle c 2b 2b by the best coders in the world

showing results for - "pascal triangle c 2b 2b"
Martin
28 May 2020
1#include <iostream>
2using namespace std;
3
4int main()
5{
6    int rows, coef = 1;
7
8    cout << "Enter number of rows: ";
9    cin >> rows;
10
11    for(int i = 0; i < rows; i++)
12    {
13        for(int space = 1; space <= rows-i; space++)
14            cout <<"  ";
15
16        for(int j = 0; j <= i; j++)
17        {
18            if (j == 0 || i == 0)
19                coef = 1;
20            else
21                coef = coef*(i-j+1)/j;
22
23            cout << coef << "   ";
24        }
25        cout << endl;
26    }
27
28    return 0;
29}
Lilian
02 Sep 2020
1/*
2Author: Jeffrey Huang
3*/
4import java.util.*;
5public class PascalTriangleCreator
6{
7    public static long factorial(long n){
8        /*
9        The whole purpose of this method is to find the factorial of a number,
10        since java does not have a built in method for it. Calculating n choose 
11        r is done using factorial, and since this code will be used repeatedly,
12        it is wise to put it in a separate method.
13        */
14        long factorial;
15        if (n==0){
16            factorial=1;
17        }
18        else{
19            factorial=1;
20            for (int counter=1;counter<=n;counter++){
21                factorial=factorial*counter;
22            }
23        }
24        return factorial;
25    }
26    
27    public static long FinalValue(long n, long r){
28        //Calculates n choose r by calling the factorial method.
29        return factorial(n) / ( factorial(n-r) * factorial(r) );
30    }
31    
32 public static void main(String[] args) {
33     Scanner sc=new Scanner (System.in);
34     long rows=1;
35     long i,j;
36     while (rows!=0){
37  System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
38  rows=sc.nextLong();
39  //The following while loop ensures that the user cannot input an invalid number.
40  while (rows<0||rows>20){
41      System.out.println("Invalid input.");
42      System.out.println("How many rows of Pascal's triangle would you like to print? (0 to stop; 1-20 rows)");
43      rows=sc.nextLong();
44  }
45  /*
46  The following if else block makes the code more efficient. Otherwise, if the user 
47  enters zero at any other point than at the start of the loop, the program will go 
48  through the long process of trying to print a triangle before terminating the
49  program. 
50  
51  Using the following method, it is true that rows==0 is tested for twice, but
52  it shortens the execution time immensely. And we know that when zero is true
53  for the if statement, it is guaranteed to be true when breaking the loop.
54  */
55  if (rows==0){
56      System.out.println("Program terminated by user.");
57  }
58  else{
59  for(i = 0; i < rows; i++) {
60      //Iterates through the number of rows required.
61         for(j = 0; j <= rows-i; j++){
62           System.out.print("   ");
63            //Iterates the printing of spaces.
64         }
65         for(j = 0; j <= i; j++){
66           if ((FinalValue(i, j))>9999) {
67             System.out.print(" "); 
68           }
69           else if ((FinalValue(i, j))>999){
70             System.out.print("  "); 
71           }
72           else if ((FinalValue(i, j))>99){
73             System.out.print("   "); 
74           }
75           else if ((FinalValue(i, j))>9){
76             System.out.print("    "); 
77           }
78           else{
79            System.out.print("     "); 
80           }
81            System.out.print(FinalValue(i, j));
82            //Prints a number of spaces plus a number.
83         }
84         System.out.println();
85        }
86        }
87     }
88 sc.close();
89 
90}
91}
Luigi
11 Sep 2016
1#include <iostream>
2using namespace std;
3
4
5int fact(int num){
6    int factorial = 1;
7    for (int i = 2; i <= num; i++)
8    {
9        factorial = factorial * i;
10    }
11    return factorial;
12}
13
14int main(){
15    int n;
16    cout << "Enter number of rows: ";
17    cin >> n;
18
19    for (int i = 0; i < n; i++)
20    {
21        for (int j = 0; j <=i; j++)
22        {
23            cout << fact(i) / (fact(j) * fact(i - j)) << " ";
24        }
25        cout << endl;
26    }
27    
28}
Montserrat
31 Jul 2017
1int pascal(int row, int col) {
2  if (col == 0 || col == row) return 1;
3  else if(col == 1 || (col + 1) == row) return row;
4  else return pascal(row - 1, col - 1) + pascal(row - 1, col);
5}
queries leading to this page
pascals triangle in javapascal triangle c 2b 2b 5chow to create pascal 27s triangle in javapascal triangle program in cppprint pascal triangle in javapascal 27s triangle cpppascal 27s trianglepascal triangle using 2a in javapascal triangleright pascal triangle in javapascal triangle in java programhow to solve pascal triangle in javahow to make pascal 27s triangle in javapascals triangle in c 2b 2brow of pascal 27s triangle c 2b 2bleft pascal triangle in javajava pascal triangle programwap to design pascal triangle in javapascal diamond in javaprogram to print the pascal triangle in cpppascal triangle java codepascal 27s triangle in c 2b 2bpascal 27s triangle program in javapascals triangle codeall pascal 27s triangle pattern javapascal triangle cpppascal triangle formula c 2b 2bpascal triangle logic javac 2b 2b program to print pascal 27s trianglepascal triangle in javapascal triangle code c 2b 2bpascal 27s triangle program in c 2b 2bc 2b 2b pascal triangleefficient c 2b 2b to displaye pascal 27s trianglepascal triangle using c 2b 2bsubarray solve pascal triangle recursion java pascal triangle program in javafastest pascal triangle in c 2b 2bprogram for pascal triangle in javapascal triangle with piramid pattern outputprint pascals triangle in javac 2b 2b pascal 27s triangle c 2b 2bpascal triangle java programpascal 27s triangle javapascal 27s triangle code in javapascal algorith in javapascals triange code in cpphow to program pascal triangle in c 2b 2bprint pascal e2 80 99s triangle in javapascals triangle javarow of pascal 27s triangle in javapascal triangle in c 2b 2bhow to make pascal triangle in c 2b 2bprint pascals triange code in cpppascal triangle formula javapascal number diamond in javapascal algorithm javatriangle of pascal in c 2b 2bpascal tree javapascal triangle geekspascal triangle geeksforgeekspascal triangle from 0 in javajava program to display pascal trianglepascal e2 80 99s triangle program in javawrite a java program to display pascal 27s triangleprint pascal triangle in c 2b 2bprint pascals trianglepascal triangle code in c 2b 2bprogram to print pascal triangle in cpppascal traingle java coe easy c 2b 2b program for pascal trainglepascal trinagle in c 2b 2bc 2b 2b function display pascal trianglepascal triangle print triangle javapascal 27s triangle java codehow to print pascal triangle in javapascal triangle code pascal 27s triangle c 2b 2bc 2b 2b print pascal triangleright pascal triangle number in javapython program to create a function that prints pascal 27s triangle c 2b 2b pascal 27s trianglepascal triangle program in c 2b 2bpascals triangle using c 2b 2bpascal traingle javapascal triangle numbers c 2b 2bpascal trianglegfgprint the pattern of pascal 27s triangle in c 2b 2bpascal 27s triangle c 2b 2b programlogic of pascal triangle c 2b 2ba java program to display pascal 27s triangle pascal triangle javapascal triangle in java using arrayhow to print pascal triangle in c 2b 2bpascal 27s triangle solution javahow to program pascals trianglepascal 27s triangle stringprinting pascal triangle in javapascal 27s triangle codeprint pascal 27s trianglehow to make a pascal triangle in javaprint pascal 27s triangle in javapascal triangle c 2b 2b vectorpascal 27s triangle java programhow to make pascal e2 80 99s triangle in javaprint pascal triangle in cpppascal triangle algorithm c 2b 2bjava pascal trianglepascal triangle pattern program in javapascal triangle c 2b 2bgenerate pascal 27s triangle javapascals triangle in cpppascal triangle printer c 2b 2bpascal e2 80 99s triangle program simple code in javapascal 27s triangle pythonpascal 27s triangle in javapascal triangle pattern in javapascal triangle code in javapascal 27s pyramid javahow to print pascal 27s triangle in javapascal 27s triangle in c 2b 2b through pointerprint pascal trianglepascal 27s triangle in cpppascal 27s triangle c 2b 2bc 2b 2b program to form pascal 27s trianglepascals triangle c 2b 2bpascal trangle c 2b 2bpascal traingle code geeks for geekspascal triangle left in javapascal triangle c 2b 2b