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...
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.
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.
I need to ask a user what numbers they want to enter. They can enter as...
I need to ask a user what numbers they want to enter. They can enter as many as they like. Then inside I need to use conditionals to determine if the numbers are <=20, <=323 && > 30, >200. I can't figure out how to let the user enter as many inputs as they want. I know I need to use a loop to check each number entered and determine if it is small middle or greater but I can't...
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));...
Let A∈Mn(R)"> A ∈ M n ( R ) A∈Mn(R) such that I+A"> I + A I+A is invertible. Suppose that
Let A∈Mn(R)A∈Mn(R) such that I+AI+A is invertible. Suppose thatB=(I−A)(I+A)−1B=(I−A)(I+A)−1(a) Show that B=(I+A)−1(I−A)B=(I+A)−1(I−A) (b) Show that I+BI+B is invertible and express AA in terms of BB.
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT