Q:

Java program to generate permutation and combination of the numbers

belongs to collection: Java Basic Programs

0

Permutations are the number of arrangements or orderings of the elements within a fixed group. Combinations on the other hand, are useful when we have to find out how many groups can form from a larger number of people.

If you are forming a group from a larger group and the placement within the smaller group is important, then you want to use permutations.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to print permutation, combination of the numbers in Java

import java.util.Scanner;

public class PermutationCombinationOfNumbers
{
    public static int fact(int num)
    {
    	// initialize and declare objects.
        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);
		
        // enter numbers here.
        System.out.print("Enter Value of n : ");
        n = scan.nextInt();
        System.out.print("Enter Value of r : ");
        r = scan.nextInt();
		
        // calculate permutation and combination here.
        System.out.print("Combination of the numbers is : " +(fact(n)/(fact(n-r)*fact(r))));
        System.out.print("\nPermutation of the numbers is : " +(fact(n)/(fact(n-r))));
    }
}

Output

First run:
Enter Value of n : 7
Enter Value of r : 3
Combination of the numbers is : 35
Permutation of the numbers is : 210

Second run:
Enter Value of n : 8
Enter Value of r : 5
Combination of the numbers is : 56
Permutation of the numbers is : 6720

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to print all Armstrong numbers betwee... >>
<< Java program to print Pascal\'s triangle...