0% found this document useful (0 votes)
54 views3 pages

ASSIGNMENT# 01 Advance Algorithm

This C program uses functions to calculate permutations, combinations, and factorials based on user input. The user is prompted to choose between calculating a permutation, combination, or factorial. Based on their selection, the program calls the appropriate function, asks the user to input values for n and r, runs the calculation, and displays the result. It then prompts the user to perform another calculation or quit.

Uploaded by

sunny730009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

ASSIGNMENT# 01 Advance Algorithm

This C program uses functions to calculate permutations, combinations, and factorials based on user input. The user is prompted to choose between calculating a permutation, combination, or factorial. Based on their selection, the program calls the appropriate function, asks the user to input values for n and r, runs the calculation, and displays the result. It then prompts the user to perform another calculation or quit.

Uploaded by

sunny730009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT# 01 Advance Algorithm

//calculation of permutation,combination and factorial using function in C


#include<stdio.h>
#include<conio.h>
int factorial(int n){
int fact = 1;
int i;
for(i = n; i > 0; i--){
fact = fact*i;
}
return fact;
}
int permutation(int n,int r){
int per;
per = factorial(n)/(factorial(n-r));
return per;
}
int combination(int n,int r){
int comb; //local variable
comb = factorial(n)/(factorial(n-r)*factorial(r));
return comb; //return statement
}
int main(){
int choice;
int n,r;

while(1){
printf("\n1. Permutation\n2. Combination\n3. Factorial\n");
printf("Choose option: ");
scanf("%d",&choice);
switch(choice){ //start of switch statement
case 1:
printf("Enter value of n,r: ");
scanf("%d%d",&n,&r);
//calculation of combination with function call
printf("Permutation P(%d,%d) = %d",n,r,permutation(n,r));
break;
case 2:
printf("Enter value of n,r: ");
scanf("%d%d",&n,&r);
printf("Combination C(%d,%d) = %d",n,r,combination(n,r));
break;
case 3:
printf("Enter value of n: ");
scanf("%d",&n);
printf("The factorial %d! = %d",n,factorial(n));
break;
default: //if user enter other than 1 or 2
printf("Invalid choice! Enter either 1 or 2");
} //end of switch statement
printf("\nWant another calculation?(y/n):");
if(getche()=='n')

break;
}
return 0;
}//end of main

You might also like