Program to calculate the value of nPr

Program to calculate the value of nPr in c

#include<stdio.h>
#include<conio.h>
int Factorial(int fact);
void Calculate_nPr(int n,int r);
void main()
{
	int n,r;
	printf("Enter the value of n \n");
	scanf("%d",&n);
	printf("Enter the value of r \n");
	scanf("%d",&r);
	Calculate_nPr(n,r);
	getch();
}
void Calculate_nPr(int n,int r)
{
	int nPr=Factorial(n)/(Factorial(n-r));
	printf("nPr Result = %d",nPr);
}
int Factorial(int fact)
{
    int total = 1;
    for (int i = fact; i >= 1; i--)
    {
        total *= i;
    }
    return total;
}

Output

Enter the value of n
5
Enter the value of r
2
nPr Result = 20

Program to calculate the value of nPr in c++

#include<iostream>
#include<conio.h>
using namespace std;
class nPr{
public:
void Calculate_nPr(int n,int r)
{
	int nPr=Factorial(n)/(Factorial(n-r));
	cout<<"nPr Result = "<<nPr;
}
public:
int Factorial(int fact)
{
    int total = 1;
    for (int i = fact; i >= 1; i--)
    {
        total *= i;
    }
    return total;
}
};
void main()
{
	int n,r;
	cout<<"Enter the value of n \n";
	cin>>n;
	cout<<"Enter the value of r \n";
	cin>>r;
	nPr npr;
	npr.Calculate_nPr(n,r);
	getch();
}

Output

Enter the value of n
5
Enter the value of r
3
nPr Result = 60

Program to calculate the value of nPr in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nPrProject
{
    class nPr
    {
       public void Calculate_nPr(int n,int r)
        {
	        int nPr=Factorial(n)/(Factorial(n-r));
	        Console.WriteLine("nPr Result = "+nPr);
        }
        public int Factorial(int fact)
        {
            int total = 1;
            for (int i = fact; i >= 1; i--)
            {
                total *= i;
            }
            return total;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the value of n");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the value of r");
            int r = Convert.ToInt32(Console.ReadLine());
            nPr npr = new nPr();
            npr.Calculate_nPr(n, r);
            Console.ReadLine();
        }
    }
}

Output

Enter the value of n
6
Enter the value of r
2
nPr Result = 30

Program to calculate the value of nPr in java

import java.util.*;
class nPr
{
    public void Calculate_nPr(int n,int r)
    {
        int nPr=Factorial(n)/(Factorial(n-r));
        System.out.println("nPr Result = "+nPr);
    }
    public int Factorial(int fact)
    {
        int total = 1;
        for (int i = fact; i >= 1; i--)
        {
            total *= i;
        }
        return total;
    }
}
class examples
{
public static void main(String[] args)
	{
	 nPr npr=new nPr();
	 Scanner sc=new Scanner(System.in);
	 System.out.println("Enter the value of n");
	 int n=sc.nextInt();
	 System.out.println("Enter the value of r");
	 int r=sc.nextInt();
	 npr.Calculate_nPr(n, r);
	}
}

Output

Enter the value of n
7
Enter the value of r
3
nPr Result = 210

Top comments

Avatar0

Sovan 27-03-2021

Nice

Write a comment


Name Email Message