Skip to main content

nCr AND nPr

nCr and nPr

#include < stdio.h >

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

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

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

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

  printf("%dC%d = %ld\n", n, r, ncr);
  printf("%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 );
}

Comments

Popular posts from this blog

CALCULATE PERCENTAGE

Calculate percentage #include < stdio.h > void main() { int s1, s2, s3, s4, s5, sum, total = 500; float per; printf("\nEnter marks of 5 subjects : "); scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5); sum = s1 + s2 + s3 + s4 + s5; printf("\nSum : %d", sum); per = (sum * 100)/500; /* percentage formula*/ printf("\nPercentage : %f", per); }

SIMPLE INTEREST

Simple interest #include < stdio.h > void main() { int amount, rate, time, ans; printf("\nEnter Principal Amount : "); scanf("%d", &amount); printf("\nEnter Rate of Interest : "); scanf("%d", &rate); printf("\nEnter Period of Time : "); scanf("%d", &time); ans = (amount * rate * time)/100; /*Simple interest formula*/ printf("\nSimple Interest : %d",ans); }

HCF AND LCM

HCF and LCM #include < stdio.h > long gcd(long, long); int main() {   long x, y, hcf, lcm;   printf("Enter two integers\n");   scanf("%ld%ld", &x, &y);   hcf = gcd(x, y);   lcm = (x*y)/hcf;   printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);   printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);   return 0; } /*if 1st no is 0 then 2nd no is gcd make 2nd no 0 by subtracting smallest from largest and return 1st no as gcd*/ long gcd(long x, long y) {   if (x == 0) {   return y;   }   while (y != 0) {   if (x > y) {     x = x - y;   }   else {     y = y - x;   } } return x; }