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

Commit

Permalink
Create print_inverted_pyramid_pattern.rs
Browse files Browse the repository at this point in the history
Adding Rust function to print an inverted pyramid pattern for issue #3668
  • Loading branch information
kenroulier authored Dec 1, 2023
1 parent 0a6da16 commit 8bc6d24
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Rust function to print an inverted pyramid pattern
//Example:
// Input : 5
// Output :
// 1 2 3 4 5
// 1 2 3 4
// 1 2 3
// 1 2
// 1

use std::io;

fn print_inverted_triangle(n: u32) {

let mut limit = n+1;

for _i in 1 .. n+1 {

for j in 1 .. limit {
print!("{} ", j);
}

limit = limit - 1;
println!();
}
}

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_inverted_triangle(n);
}

0 comments on commit 8bc6d24

Please sign in to comment.