C# program to calculate the NPR

Here, we are going to learn how to calculate the NPR in C#?
Submitted by Nidhi, on October 11, 2020

Here, we will calculate the NPR using the below formula.

NPR =  n!/(n-r)!;

Program:

The source code to calculate the NPR is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to calculate the NPR.

using System; 

class Npr
{
    static int CalculateFactorial(int n)
    {
        int fact =1;

        for (int i = 2; i <= n; i++)
        {
            fact = fact * i;
        }

        return fact;
    }

    static int CalculateNpr(int n, int r)
    {
        int npr     = 0;
        int fact1   = 0;
        int fact2   = 0;

        fact1 = CalculateFactorial(n);
        fact2 = CalculateFactorial(n - r);

        npr = fact1 / fact2;

        return npr;
    }
    static void Main(string[] args)
    {
        int npr  = 0;
        int n  = 0;
        int r  = 0;

        Console.Write("Enter the value of 'n': "); 
        n = int.Parse(Console.ReadLine());

        Console.Write("Enter the value of 'r': ");
        r = int.Parse(Console.ReadLine());
        
        npr = CalculateNpr(n, r);

        Console.WriteLine("Npr: "+npr);
    }
}

Output:

Enter the value of 'n': 10
Enter the value of 'r': 5
Npr: 30240
Press any key to continue . . .

Explanation:

Here, we created a class Npr that contains three static methods CalculateFactorial(), CalculateNpr(), and Main() method.

The CalculateFactorial() is used to calculate the factorial of the specified factorial and return to the calling method.

The CalculateNpr() is used to calculate the Npr using the below formula.

NPR =  n!/(n-r)! ;

The Main() method is the entry point for the program, Here we read the value of n and r and called the CalculateNpr() method that will return NPR that will be printed on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.