Code it! Beginner’s Turbo C++

1
437

 

Well in this fast pacing world, computer language has turned out to be a life saver. Ranging from schools to colleges, mini-startups to MNC’s, there isn’t any place where computer language, be it- hardware or software isn’t used even a bit. So for starters, here’s a basic programming code in Turbo C++ language, one of the most commonly used computer language. Point to remember is that this language strictly goes around with the set of logic and is highly case sensitive.

The following code/program is to find permutation and combination of a no. entered. The details of program statements would be explained at the steps needed by the // sign.

#include<iostream.h>

long fact(int n);                               //declarations of functions needed

long per(int n, int r);

long comb(int n, int r);

int main()                 // main() function execution takes place here

{

int n,r;

long perm,com;

cout<<“Enter values of n and r”;

cin>>n>>r;

perm=per(n,r);

com=comb(n,r);

cout<<“Permutation is  “;

cout<<perm<<endl;

cout<<“Combination is “;

cout<<com;

return 0;

}

long fact(int n)                    // function to find factorial of a number

{

long fact=1.0;

for(int i=1;i<=n;i++)

fact*=i;

return fact;

}

long per(int n, int r)     // function to find permutation [nPr=n!/(n-r)!]

{

long res=(fact(n)/fact(n-r));

return res;

}

long comb(int n, int r)                  // function to find combination [nCr=n!/{r!*(n-r)!)]

{

return (fact(n)/(fact(r)*fact(n-r)));

}

Explanation

The program is a basic example to find permutation and combination of a number without actually manually calculating it. Both depends on the factorial of the numbers used (n and r) hence function fact() is to find factorial. Factorial of a number is calculated as n*(n-1)*(n-2)*…*1 and is denoted by n!, for example factorial of 5(5!) is 120 (5*4*3*2*1).

Next step is to use the fact() function to find permutation and combination . The general formula for permutation P(n,r) can be written as nPr=n!/(n-r)! while combination C(n,r) is nCr=n!/{r!*(n-r)!}. All functions used in this program are return type i.e., they return values of specific data type.

Examples

Values (n, r) Permutation Combination
n=5, r=3 60 10
n=7, r=4 840 35
n=3, r=2 6 3

 

Output of the given program

Enter values of n and r

5 3

Permutation is 60

Combination is 10

1 COMMENT

  1. I have read so many articles or reviews concerning the blogger lovers however this post is really a good article, keep it up.

Comments are closed.