Question

In Java

Implement a program that reads two integers (n and k). The program must output the binomial coefficient as follows: Inn! (k)k

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.Scanner;
public class factorial {

//declaration of function fact()
//used to find the factorial of the given number
public static long fact(int n)
   {
       //factorial of 1 and 0 is equal to 1
       //if value is less than equal to 1 return 1
       if(n <= 1)
       return 1;
       else
       //recursive function fact() to find factorial
       //This recuraion repeats upto n become 1
       //when value become 1 then it starts return the value and returned values is multiplied with value in variable n.finally after all iteration it return the final result to called function.
       return n * fact(n - 1);
      
   }
   //main starts here
public static void main(String[] args) {
  
   //Scanner class used to get the input from user
   Scanner s = new Scanner(System.in);
   System.out.print("Enter n value: ");
   //Enter the value of n
   int n = s.nextInt();
   System.out.print("Enter k value: ");
   //Enter the value of k
   int k = s.nextInt();
  
   long result;
   //calling the function fact()
   // binomial coefficient = n!/(k!(n-k)!)
   result = fact(n)/(fact(k)*fact(n-k));
   //Displays the result for binomial coefficient
   System.out.println("Result: "+result);
   System.out.print("Coefficient-array:");
   //Displays the result for everu coefficient that is before bionimal coefficient for given k
   for(int i = 0; i<= k;i++)
   {
      result = fact(n)/(fact(i)*fact(n-i));
      System.out.print(" "+result);
   }
}
}

import java.util.Scanner; public class factorial { //declaration of function facto //used to find the factorial of the giventhe value and returned values is multiplied with value in variable n. finally after all iteration it return the final resultEnter n value: 5 Enter k value: 3 Result: 10 Coefficient-array: 15 10 10Enter n value: 10 Enter k value: 4 Result: 210 Coefficient-array: 1 10 45 120 210

Add a comment
Know the answer?
Add Answer to:
In Java Implement a program that reads two integers (n and k). The program must output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads k integers and displays their sum. Write a program that reads...

    Write a program that reads k integers and displays their sum. Write a program that reads k values representing family income from the keyboard and place them into the array. Find the maximum income among these values. Count the families that make less than 10 percent of the maximum income. (JAVA)

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

  • Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b,...

    Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return (2x - 12y)^4 = 16x^4 - 384x^3y + 3456x^2y^2 – 13824xy^3 + 20736y^4 Use the Pascal’s triangle method using only one 1-dim array to calculate all binomial coefficients. 1. Write a JAVA method that expands a given binomial (ax by)",...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • Please do in C and only use stdio.h. Write a program that reads n integers into...

    Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...

  • in JAVA program.Use top-down design to design and implement a program to ask the user to...

    in JAVA program.Use top-down design to design and implement a program to ask the user to enter a list of integers, and then display to the user the mean and median of this list. You should first prompt the user to specify the length of the list of integers. For this assignment, your code should create an array of size 10, and then allow the user to specify the number of integers in their list, up to a maximum of...

  • Use DevC++ to implement a program that can store and output 5 integers, where the user...

    Use DevC++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. (5 points) 2. Output the content (value) that stored in the array of five elements. (10 points) 3. Continue to ask the user to store for storing data into another array(store and output 5 integers). (10 points) Prelude to Programming...

  • [C++] Write a program that reads a list of integers, and outputs those integers in reverse....

    [C++] Write a program that reads a list of integers, and outputs those integers in reverse. You must use an array for this lab. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 5 To achieve the above, first read the integers into...

  • Write a Java program that reads in 5 words from the user and stores these words...

    Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information: All words are in reverse order (based both on content and order). Every word at an even index of the array (i.e., indices 0, 2, and 4). For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two...

  • Use C++ to implement a program that can store and output 5 integers, where the user...

    Use C++ to implement a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. (5 points) 2. Output the content (value) that stored in the array of five elements. (10 points) 3. Continue to ask the user to store for storing data into another array. (10 points)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT