C++ Program to calculate nPr & nCr. This program calculates the value of nCr and nPr. The values of n and r
are input by user. We have used 3 functions : 'npr' for calculating nPr ,
'ncr' for calculating nCr , factorial to calculate factorial. nPr is
calculated by the formula nPr = factorial(n)/factorial(n-r)
, nCr by factorial(n)/(factorial(r)*factorial(n-r)). The values are calculated by these functions and factorial by 'factorial'. The results are printed using cout.
Source Code:-
#include<iostream.h>
#include<conio.h>
long fnpr(int, int);
long fncr(int, int);
long factorial(int);
void main()
{
int n, r;
long ncr, npr;
clrscr();
cout<<"Calculate nCr & nPr by-Tarun Rawat\n";
cout<<"\nEnter the value of n : ";
cin>>n;
cout<<"Enter the value of r : ";
cin>>r;
ncr=fncr(n,r);
npr=fnpr(n,r);
cout<<"\nValue of "<<n<<"C"<<r<<" = "<<ncr;
cout<<"\nValue of "n<<"P"<<r<<" = "<<npr;
getch();
}
long fnpr(int n,int r)
{
long value;
value=factorial(n)/factorial(n-r);
return value;
}
long fncr(int n,int r)
{
long value;
value=factorial(n)/(factorial(r)*factorial(n-r));
return value;
}
long factorial(int n)
{
if(n==0)
return 1;
else
{
return(n*factorial(n-1));
}
}
, nCr by factorial(n)/(factorial(r)*factorial(n-r)). The values are calculated by these functions and factorial by 'factorial'. The results are printed using cout.
Source Code:-
#include<iostream.h>
#include<conio.h>
long fnpr(int, int);
long fncr(int, int);
long factorial(int);
void main()
{
int n, r;
long ncr, npr;
clrscr();
cout<<"Calculate nCr & nPr by-Tarun Rawat\n";
cout<<"\nEnter the value of n : ";
cin>>n;
cout<<"Enter the value of r : ";
cin>>r;
ncr=fncr(n,r);
npr=fnpr(n,r);
cout<<"\nValue of "<<n<<"C"<<r<<" = "<<ncr;
cout<<"\nValue of "n<<"P"<<r<<" = "<<npr;
getch();
}
long fnpr(int n,int r)
{
long value;
value=factorial(n)/factorial(n-r);
return value;
}
long fncr(int n,int r)
{
long value;
value=factorial(n)/(factorial(r)*factorial(n-r));
return value;
}
long factorial(int n)
{
if(n==0)
return 1;
else
{
return(n*factorial(n-1));
}
}
give me heap sort with randomize function boy!!!
ReplyDelete