# October 14, 2008 Combination and Permutation

Permutation And Combination using C
*Permutation an Combination
this program to found permutation and combination of number
using C, we contruct using DevC++, you can use visual C, turbo C and many other
but maybe some funtion is not working
we recomended using DevC++ v 4.9
*/
#include “stdio.h”
#include “conio.h”
#include “stdlib.h”
#include “string.h”
#define MAX 20
/*it is function to calculate the permutation and combination using factorial*/
int factorial(int q)
{
if (q==0)
return(1);
else
return(q*factorial(q-1));/*this recursive funtions*/
}
/*this function to calculate the permutation*/
void permutation()
{
system(“cls”);/*clear konsole */
printf(“\tPermutation and Combination\n”);
printf(“\t===========================\n\n”);
char string[MAX];/*declare of array*/
printf(“input : “);
gets(string);/*take the string for string*/
int long_of_string = strlen(string);/*to get the long of the string, it use for looping*/
int flag=0;/*it for flag and index the ‘p’ position in the array*/
for(int i=0;i {
if(string[i]==’p’ || string[i]==’P’)/*try to find identifier p*/
{
flag=i;break;/*save the index of p in the array to flag*/
}
}
if(flag!=0)/*if the char p was found*/
{
int firts_number=atoi(string);/*convert the first number until char p*/
int second_number= atoi(&string[flag+1]);/*convert the second number begin from
char p*/
if(firts_number>=second_number)
{
int factorial_first_number=factorial(firts_number);/*calculate factorial of
first_number*/
int factorial_second_number=factorial(second_number);/*calculate factorial of
second_number*/
printf(“output : %d\n\n”,factorial_first_number/factorial_second_number);/*display
permutation of firts and second number*/
}
else {printf(“output : Imposible\n\n”);}/*if the first number less than the second
number that imposible to permutation*/
}
else{printf(“\nWrong\n”);}/*if the comand without p indentity, it display wrong
messege*/
}
/*this functions to calculate the combination*/
void combination()
{
system(“cls”);
printf(“\tpermutation and combination\n”);
printf(“\t===========================\n\n”);
char string[MAX];
printf(“input : “);
gets(string);
int long_of_string = strlen(string);/*calculate the long of string*/
int flag=0;/*it variabel to take index of char ‘c’ in the array*/
for(int i=0;long_of_string>i;i++)/*looping for found ‘c’*/
{
if(string[i]==’c’ || string[i]==’C’)/*if found char c*/
{
flag=i;break;
}
}
if(flag!=0)
{
int firts_number=atoi(string);/*convert the first number until char p*/
int second_number= atoi(&string[flag+1]);/*convert the second number begin from
char p*/
if(firts_number>second_number)
{
int factorial_of_third_number=factorial(firts_number-second_number);
int factorial_of_firts_number=factorial(firts_number);
int factorial_of_second_number=factorial(second_number);
printf(“output : %d\n\n”, factorial_of_firts_number/(factorial_of_third_number*
factorial_of_second_number));
}
else {printf(“output : imposible\n\n”);}/*if the first number less than the second
number that imposible to permutation*/
}
else{printf(“\nwrong code\n”);}/*if the comand without p indentity, it display wrong
messege*/
}
/*this is the main program*/
main(void)
{ char u;
do
{
system(“cls”);
printf(“\tpermutation and combination\n”);
printf(“\t===========================\n\n”);
printf(“1.PERMUTATION (ex : 12p5)\n2.COMBINATION (ex : 5c2 )\n\n”);
char p;
p=getch();/*try to take the choose of user*/
switch(p)
{
case ‘1’ : permutation();break;/*go to permutation function*/
case ‘2’ : combination();break;/*go to combination function*/
default : printf(“\nWRONG\n”);break;
}
printf(“PRESS Y FOR REPEAT : “);
u=getch();/*stop until user press any key*/
}while(u==’y’||u==’Y’);
}