Q:

Write a C program to find nCr and nPr using function

0

Write a C program to find nCr and nPr using function. Here’s simple program to find nCr and nPr using function in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Below is the source code for C program to find nCr and nPr using function which is successfully compiled and run on Windows System to produce desired output as shown below :

 
 


SOURCE CODE : :

/*  C program to find nCr and nPr using function  */

#include <stdio.h>

long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

int main()
{
   int n, r;
   long ncr, npr;

   printf("Enter the value of n :: ");
   scanf("%d",&n);
   printf("\nEnter the value of r :: ");
   scanf("%d",&r);

   ncr = find_ncr(n, r);
   npr = find_npr(n, r);

   printf("\n%dC%d = %ld\n", n, r, ncr);
   printf("\n%dP%d = %ld\n", n, r, npr);

   return 0;
}

long find_ncr(int n, int r) {
   long result;

   result = factorial(n)/(factorial(r)*factorial(n-r));

   return result;
}

long find_npr(int n, int r) {
   long result;

   result = factorial(n)/factorial(n-r);

   return result;
}

long factorial(int n) {
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
      result = result*c;

   return result;
}

OUTPUT : :


/*  C program to find nCr and nPr using function  */

Enter the value of n :: 4

Enter the value of r :: 2

4C2 = 6

4P2 = 12

Process returned 0

Above is the source code for C program to find nCr and nPr using function which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Number Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to swap two numbers using third variable... >>
<< C program to generate random numbers using rand fu...