ADVERTISEMENT
ADVERTISEMENT

Rust program to calculate the value of nPr

Given the value of n, r, and we have to calculate the value of nPr using the Rust program.
Submitted by Nidhi, on October 01, 2021

Problem Solution:

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)!

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 n, r from the user. After that, we calculated the nPr and print the result.

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.