Java program to generate permutation and combination of the numbers

In this java program, we are going to generate permutation and combination of the numbers.
Submitted by IncludeHelp, on November 17, 2017

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.

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

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.