In: Computer Science
My question is if n<r when there is no loop. I can't enter numbers again.Where is the problem? How do it?
#include
int fact(int no)
{
if (no == 0)
return 1;
return no * fact(no - 1);
}
int calc(int n, int r,int *com,int *per) //as given in question
formula of permutation n!/(n-r)!
{
*com=fact(n)/(fact(r)*fact(n-r));
*per=fact(n)/fact(n-r);
}
int main()
{
int n=0,r=0,co,pe;
while(n<=0 || r<=0)
{
printf("Enter N value:");
scanf("%d",&n);
printf("Enter R value:");
scanf("%d",&r);
if(n {
printf("N value should be greater than R value! try again.");
return(0);
}
else if(n<0 || r<0)
{
printf("N or R values should be equal or greater than Zero.
Bye!\n");
return(0);
}
}
calc(n,r,&co,&pe);
printf("Combination : ( %d,%d ) = %.2d \n",n,r,co);
printf("permutation : ( %d,%d ) = %.2d \n",n,r,pe);
}
#include<stdio.h>
int fact(int no)
{
if (no == 0)
return 1;
return no * fact(no - 1);
}
int calc(int n, int r,int *com,int *per) //as given in question formula of permutation n!/(n-r)!
{
*com=fact(n)/(fact(r)*fact(n-r));
*per=fact(n)/fact(n-r);
}
int main()
{
int n=0,r=0,co,pe;
// infinite loop
while(true)
{
printf("Enter N value:");
scanf("%d",&n);
printf("Enter R value:");
scanf("%d",&r);
// if n is smaller than equal to r
if( n <= r ){
printf("N value should be greater than R value! try again.");
// as we want to loop again, we will nit use return as it will terminate the program
// we wil loop again to get user input which is correct
// return(0);
}
else if( n<0 || r<0 )
{
printf("N or R values should be equal or greater than Zero. Bye!\n");
return(0);
}
// if both the values are valid, exit the loop
else if( n > 0 && r > 0 )
break;
printf("\n\n");
}
calc(n,r,&co,&pe);
printf("Combination : ( %d,%d ) = %.2d \n",n,r,co);
printf("permutation : ( %d,%d ) = %.2d \n",n,r,pe);
}
Sample Output