Q:

Rust program to calculate the value of nPr

belongs to collection: Rust Basic Programs

0

Here, we will read the value of n and r from the user. Then we will calculate the nPr and print the result.

nPr:

The nPr is the permutation of arrangement of 'r' objects from a set of 'n' objects, into an order or sequence.

The formula to find permutation is: nPr = (n!) / (n-r)!

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to calculate the value of nPr is given below. The given program is compiled and executed successfully.

// Rust program to calculate the value of nPr

use std::io;

fn getFactorial(num:i32)->i32
{
    let mut f:i32 = 1;
    let mut i:i32 = 1;

    if (num == 0)
    {
        return 1;
    }
    
    while(i <= num) 
    {    
        f = f * i;
        i=i+1;
    }
    return f;
}

fn main() 
{
    let mut n:i32   =0;
    let mut r:i32   =0;
    let mut nPr:i32 =0;
   
    let mut input1 = String::new();
    let mut input2 = String::new();
    
    println!("Enter value of n: ");
    io::stdin().read_line(&mut input1).expect("Not a valid string");
    n = input1.trim().parse().expect("Not a valid number");
    
    println!("Enter value of r: ");
    io::stdin().read_line(&mut input2).expect("Not a valid string");
    r = input2.trim().parse().expect("Not a valid number");

    nPr = getFactorial(n) / getFactorial(n - r);
    println!("The nPr is: {}", nPr);
}

Output:

Enter value of n: 
6
Enter value of r: 
4
The nPr is: 360

Explanation:

Here, we read the value of nr from the user. After that, we calculated the nPr and print the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to extract the last two digits from a... >>
<< Rust program to calculate the value of nCr...