ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.