codescracker


java

Java Program to Find ncR nPr



« Previous Program Next Program »


Find ncR nPr

To find ncR and nPr in Java Programming, you have to ask to the user to enter the value of n and r to find the ncR and nPr to display the value of ncR and nPr on the screen as shown in the following program.

Java Programming Code to Find ncR nPr

Following Java Program to ask the user to enter two number i.e., the value of n and the value of r, to find its ncR and nPr, then display the result on the screen:

/* Java Program Example - Find ncR nPr */
		
import java.util.Scanner;

public class JavaProgram
{
    public static int fact(int num)
    {
        int fact=1, i;
        for(i=1; i<=num; i++)
        {
            fact = fact*i;
        }
        return fact;
    }
    public static void main(String args[])
    {
        int n, r;
        Scanner scan = new Scanner(System.in);
		
        System.out.print("Enter Value of n : ");
        n = scan.nextInt();
        System.out.print("Enter Value of r : ");
        r = scan.nextInt();
		
        System.out.print("NCR = " +(fact(n)/(fact(n-r)*fact(r))));
        System.out.print("\nNPR = " +(fact(n)/(fact(n-r))));
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program find ncr npr

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test