Having some issues with this assigment, please help. - The assignment is to crea
ID: 3566098 • Letter: H
Question
Having some issues with this assigment, please help.
- The assignment is to create a program to display the number of combinations where n and k are entered by the user.
Input 1.
a) Ask for n. Check for bounds (1 <= n && n <= 10).
i) If the user entered an integer within (1 and 10), move on to getting the value for k
ii) Else keep asking the user till they give a correct value which is an integer within 1 and 10.
iii) Reject any input greater than 10 or less than 1 by displaying an error message and then asking the user to re-enter the input. Do this as many times that they enter incorrect input.
Input 2.
Ask for k. Check for bounds (1 <= k && k <= n).
If the user entered an integer within (1 and k), move on to next step.
Else keep asking the user till they give a correct value which is an integer within 1 and n.
Reject any input greater than n or less than 1 by displaying an error message and then asking the user to re-enter the input. Do this as many times that they enter incorrect input.
Calculate n
Explanation / Answer
#include<stdio.h>
int factorial(int num){
if(num ==0 || num == 1){
return 1;
}
int fact = 1;
while(num > 1){
fact *= num;
num--;
}
return fact;
}
int main(){
int n,k;
int flag = 0;
while(!flag){
printf("Enter the value of n : ");
scanf("%d",&n);
if(n >= 1 && n <= 10){
flag = 1;
break;
}else{
printf("Value of n should be between 1 and 10 . Please try again with anothere value. ");
}
}
flag = 0;
while(!flag){
printf("Enter the value of k : ");
scanf("%d",&k);
if(k >= 1 && k <= n){
flag = 1;
break;
}else{
printf("Value of k should be between 1 and %d.Please try again with anothere value. ",n);
}
}
int factn = factorial(n);
int factk = factorial(k);
int factnk = factorial(n-k);
int res = factn/(factk * factnk);
printf("The number of combinations of %d things taken %d at a time is %d ",n,k,res);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.