Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

Tuesday, October 4, 2011

Program to find the value of nCr





Write a program to find nCr....



Program:

#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
int n,r;
long int ncr;
clrscr();
l1:printf("Enter value of n and r to find the value of ncr(n>r)");
scanf("%d%d",&n,&r);
if(n<r)
{
printf("Enter the value of n>r \n.\n.\n.\n.\n.\n.\n.\n.\n.\n.");
goto l1;
}
ncr=factorial(n)/(factorial(r)*factorial(n-r));
printf("Value of nCr : %ld",ncr);
getch();
}

long int factorial(int n)
{
return n>1?n*factorial(n-1):1;
}



Output:




Read full history - Program to find the value of nCr

Finding Factorial of a number [ using Recursion & Iteration ]


 Write a program to find the factorial of a given number using recursion and iteration .....

Program
:

Using recursion...


#include<stdio.h>
#include<conio.h>
long int recur_factorial(int n);
void main()
{
int n;
long int fact;
clrscr();
printf("Enter the number to find the factorial of :");
scanf("%d",&n);
fact=recur_factorial(n);
printf("\nFactorial of %d is %ld",n,fact);
getch();
}

long int recur_factorial(int n)
{
return n>1?n*recur_factorial(n-1):1;
}


Using iteration....



#include<stdio.h>
#include<conio.h>
int fact_it(int i, int f);
void main()
{
int f=1,i,x;
clrscr();
printf("Enter the number to find the factorial of:");
scanf("%d",&i);
x=i;
for(i;i>0;i--)
f=fact_it(i,f);
printf("\nFactorial of %d is %d ",x,f);
getch();
}

int fact_it (int i,int f)
{
f=f*i;
return f;
}


Output:






Read full history - Finding Factorial of a number [ using Recursion & Iteration ]

About Me