codescracker


c++

C++ Program to Find ncR nPr



« Previous Program Next Program »


Find ncR and nPr in C++

To find ncR and nPr in C++ programming, you have to ask to the user to enter the value of n and r to find the ncR and nPr, and display the value of ncR and nPr on the output screen as shown here in the following program.

C++ Programming Code to Find ncR nPr

Following C++ program to ask to the user to enter the two number (and store these two number in variables say n and r), to find its ncR and nPr, then display the result on the screen:

/* C++ Program - Find ncR and nPr */
		
#include<iostream.h>
#include<conio.h>
long int fact(int);   //function declaration
void main()
{
	clrscr();
	int n, r;
	long int ncr, npr;
	cout<<"Enter the value of n : ";
	cin>>n;
	cout<<"Enter the value of r : ";
	cin>>r;
	npr=fact(n)/fact(n-r);    // calling the function or function calling
	ncr=npr/fact(r);          //function calling
	cout<<"NPR value = "<<npr<<"\n";
	cout<<"NCR value = "<<ncr<<"\n";
	getch();
}
long int fact(int x)          //defining the function or function definition
{
	int i, f=1;
	for(i=2; i<=x; i++)
	{
		f=f*i;
	}
	return f;
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program find ncr npr

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test