Question

In: Computer Science

My question is if n<r when there is no loop. I can't enter numbers again.Where is...

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);
}

Solutions

Expert Solution

#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


Related Solutions

My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
I am struggling with this assignment. I can't get the program to run when I enter...
I am struggling with this assignment. I can't get the program to run when I enter a number with the $ symbol followed by a number below 10. any help would be greatly appreciated. Create a program named Auction that allows a user to enter an amount bid on an online auction item. Include three overloaded methods that accept an int, double, or string bid. Each method should display the bid and indicate whether it is over the minimum acceptable...
This is a question for my problem-solving class. I am really stuck and I can't see...
This is a question for my problem-solving class. I am really stuck and I can't see much of a pattern so I would appreciate if someone could draw out for each thief and explain the pattern to get the answer for 40 thieves! Question: Forty thieves, all different ages, steal a huge pile of identical gold coins and must decide how to divide them up. They settle on the following procedure. The youngest divides the coins among the thieves however...
How should I answer this question if I can't remember my dreams For a few days,...
How should I answer this question if I can't remember my dreams For a few days, record your dreams first thing in the morning. Discuss what is going on in your life related to your dream as further evidence that our dream activity is further processing of relevant information.
declare an integer array of n numbers //enter a value for n before the declaration enter...
declare an integer array of n numbers //enter a value for n before the declaration enter values for the declared array, pass the array to the method that has the name "countNumbers" let the function countNumber count how many elements of the array in the interval [70 90] means between 70 and 90 inclusive, returns this count to the main, and prints it using format printing. --------------- Please Solve As soon as Solve quickly I get you thumbs up directly...
having trouble with loop, when I added saving and loading to my code, when adding new...
having trouble with loop, when I added saving and loading to my code, when adding new player, it continues to ask for new player class TeamClass: name = "" jersey = "" number = "" def __init__(self, name, jersey, number): self.name = name self.number = number self.jersey = jersey def set_name(self, name): self.name = name def set_jersey(self, jersey): self.jersey = jersey def set_number(self, number): self.number = number def get_name(self, name): return self.name def get_jersey(self, jersey): return self.jersey def get_number(self, number):...
(Python) How would I add this input into my code? "Enter Y for Yes or N...
(Python) How would I add this input into my code? "Enter Y for Yes or N for No:" as a loop. def getMat(): mat = np.zeros((3, 3)) print("Enter your first 3 by 3 matrix:") for row in range(3): li = [int(x) for x in input().split()] mat[row,0] = li[0] mat[row,1] = li[1] mat[row,2] = li[2] print("Your first 3 by 3 matrix is :") for i in range(3): for k in range(3): print(str(int(mat[i][k]))+" ",end="") print() return mat def checkEntry(inputValue): try: float(inputValue) except...
// I can't get my cpp file to work with my header file and it always...
// I can't get my cpp file to work with my header file and it always says the error comes from compatibility I am using Visual Studio 2015 below is the error only my file path and program name has been erased. The issue comes up in my cpp file with the size_t empty and full functions. // Error (active)       declaration is incompatible with "bool ListType::empty()" (declared at line 12 of ListType.h)         // ListType header file #ifndef...
Yesterday I looked underwater with my eyes open (and no goggles) and I realized I can't...
Yesterday I looked underwater with my eyes open (and no goggles) and I realized I can't see anything clearly. Everything looks very, very blurry. My guess is that the eye needs direct contact with air in order to work properly. With water, the refraction index is different, and the eye lens are not able to compensate for correct focalization on the retina. Am I right ? If so, what lenses should one wear in order to see clearly while under...
A matlab program to enter N numbers and count the positive, negative and zero numbers. Then...
A matlab program to enter N numbers and count the positive, negative and zero numbers. Then print the results
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT