Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Create print-pascals-pyramid.rs (#4936)
Browse files Browse the repository at this point in the history
Print-pascals-pyramid closes issue #2968
  • Loading branch information
kenroulier authored Dec 12, 2023
1 parent 01e1cbd commit d6904f7
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions program/program/print-pascals-triangle/print-pascals-pyramid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Rust program to print pascal's triangle
//
//Pascal's triangle is a triangular array of the binomial coefficients. Write a function that takes an
// integer value n as input and prints first n lines of the Pascal's triangle.
//
//Input : 5
//
//Output :
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// 1 4 6 4 1

use std::io;

fn print_pascals_triangle(levels: u32)
{

// outer loop to go through all the levels
for row in 0..levels {

let mut print_num = 1; //start every row with "1"

//print spaces out before row based on num of levels

for _j in 1..2*(levels-1-row)+1 {
print!(" ");
}

// calculate binomial coefficients ==> p = p * ((row-col)/(col+1))

for calc_col in 0..row+1 {
print!("{:3} ", print_num);
print_num = print_num * (row-calc_col)/(calc_col+1);
}

println!(); //insert a newline to start the next line to print
}
}

fn main() {

// the input string and this main function can be removed. This is simply for testing

println!("Enter the number of levels to print");
let mut input_num = String::new();
let _rtrn = io::stdin().read_line(&mut input_num);
let n: u32 = input_num.trim().parse().expect("Input not an integer");

// example of how to call the function and print out the results of the inverted pyramid.
print_pascals_triangle(n);
}

0 comments on commit d6904f7

Please sign in to comment.