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.
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):...
// 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...
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
I can't seem to make my inheritance to work properly between my parent class GameCharacter and...
I can't seem to make my inheritance to work properly between my parent class GameCharacter and the child classes hero and villain. Here's my code: import java.sql.SQLOutput; public class Main { public static void main(String[] args) { GameCharacter me = new GameCharacter("King Arthur", 5); GameCharacter tree = new GameCharacter("Tall Tree", 5); GameCharacter enemy = new GameCharacter("Monster Bug", 10); System.out.println(); System.out.println("\n~~~ Game Characters Created ~~~"); System.out.println(tree); System.out.println(me); System.out.println(enemy); System.out.println("\n~~~ The Bug Has Been Attacked ~~~"); me.attack(enemy); System.out.println(tree); System.out.println(me); System.out.println(enemy); System.out.println("\n~~~ The...
How can I make my own function for rbinom(n, size, prob) in R. So, basically I...
How can I make my own function for rbinom(n, size, prob) in R. So, basically I want to build my own rbinom function that gives me the same result as rbinom that's built in R software.
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT