Permutation coefficient program
The permutation coefficient, also known as a permutation or factorial coefficient, is a mathematical concept used to count the number of ways that a set of distinct objects can be arranged in a specific order. It is denoted by the symbol "P" and is calculated by multiplying the number of objects being arranged by the number of objects that can fill the next position, and so on, until all positions have been filled.
The formula for the permutation coefficient is:
P(n, r) = n! / (n - r)!
where "n" is the total number of objects and "r" is the number of objects being arranged. The exclamation mark represents the factorial function, which means multiplying a sequence of descending positive integers.
For example, if you have 4 distinct objects (A, B, C, and D) and you want to arrange them in a line of 3, the permutation coefficient would be:
P(4, 3) = 4! / (4-3)! = 4 x 3 x 2 = 24
This means that there are 24 possible ways to arrange the 4 objects in a line of 3, such as ABC, ABD, BAC, BDA, CAD, etc.
Code Solution
/*
C program for
Permutation coefficient program
*/
#include <stdio.h>
void permutationCoefficient(int n, int k)
{
// Set initial value
int p = 1;
for (int i = n; i >= (n - k + 1); --i)
{
p = p *i;
}
// Display calculated results
printf(" ( n = %d, k = %d ) : %d \n", n, k, p);
}
int main(int argc, char
const *argv[])
{
// Test samples
permutationCoefficient(7, 3);
permutationCoefficient(4, 2);
return 0;
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
/*
Java Program for
Permutation coefficient program
*/
public class Coefficient
{
public void permutationCoefficient(int n, int k)
{
// Set initial value
int p = 1;
for (int i = n; i >= (n - k + 1); --i)
{
p = p * i;
}
// Display calculated results
System.out.print(" ( n = " + n + ", k = " + k + " ) : " + p + " \n");
}
public static void main(String[] args)
{
Coefficient task = new Coefficient();
// Test samples
task.permutationCoefficient(7, 3);
task.permutationCoefficient(4, 2);
}
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Permutation coefficient program
*/
class Coefficient
{
public: void permutationCoefficient(int n, int k)
{
// Set initial value
int p = 1;
for (int i = n; i >= (n - k + 1); --i)
{
p = p *i;
}
// Display calculated results
cout << " ( n = " << n << ", k = " << k << " ) : " << p << " \n";
}
};
int main()
{
Coefficient *task = new Coefficient();
// Test samples
task->permutationCoefficient(7, 3);
task->permutationCoefficient(4, 2);
return 0;
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
// Include namespace system
using System;
/*
Csharp Program for
Permutation coefficient program
*/
public class Coefficient
{
public void permutationCoefficient(int n, int k)
{
// Set initial value
int p = 1;
for (int i = n; i >= (n - k + 1); --i)
{
p = p * i;
}
// Display calculated results
Console.Write(" ( n = " + n + ", k = " + k + " ) : " + p + " \n");
}
public static void Main(String[] args)
{
Coefficient task = new Coefficient();
// Test samples
task.permutationCoefficient(7, 3);
task.permutationCoefficient(4, 2);
}
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
package main
import "fmt"
/*
Go Program for
Permutation coefficient program
*/
func permutationCoefficient(n, k int) {
// Set initial value
var p int = 1
for i := n ; i >= (n - k + 1) ; i-- {
p = p * i
}
// Display calculated results
fmt.Print(" ( n = ", n, ", k = ", k, " ) : ", p, " \n")
}
func main() {
// Test samples
permutationCoefficient(7, 3)
permutationCoefficient(4, 2)
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
<?php
/*
Php Program for
Permutation coefficient program
*/
class Coefficient
{
public function permutationCoefficient($n, $k)
{
// Set initial value
$p = 1;
for ($i = $n; $i >= ($n - $k + 1); --$i)
{
$p = $p * $i;
}
// Display calculated results
echo(" ( n = ".$n.", k = ".$k." ) : ".$p." \n");
}
}
function main()
{
$task = new Coefficient();
// Test samples
$task->permutationCoefficient(7, 3);
$task->permutationCoefficient(4, 2);
}
main();
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
/*
Node JS Program for
Permutation coefficient program
*/
class Coefficient
{
permutationCoefficient(n, k)
{
// Set initial value
var p = 1;
for (var i = n; i >= (n - k + 1); --i)
{
p = p * i;
}
// Display calculated results
process.stdout.write(" ( n = " + n +
", k = " + k + " ) : " + p + " \n");
}
}
function main()
{
var task = new Coefficient();
// Test samples
task.permutationCoefficient(7, 3);
task.permutationCoefficient(4, 2);
}
main();
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
# Python 3 Program for
# Permutation coefficient program
class Coefficient :
def permutationCoefficient(self, n, k) :
# Set initial value
p = 1
i = n
while (i >= (n - k + 1)) :
p = p * i
i -= 1
# Display calculated results
print(" ( n = ", n ,", k = ", k ," ) : ", p,sep="" )
def main() :
task = Coefficient()
# Test samples
task.permutationCoefficient(7, 3)
task.permutationCoefficient(4, 2)
if __name__ == "__main__": main()
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
# Ruby Program for
# Permutation coefficient program
class Coefficient
def permutationCoefficient(n, k)
# Set initial value
p = 1
i = n
while (i >= (n - k + 1))
p = p * i
i -= 1
end
# Display calculated results
print(" ( n = ", n ,", k = ", k ," ) : ", p ," \n")
end
end
def main()
task = Coefficient.new()
# Test samples
task.permutationCoefficient(7, 3)
task.permutationCoefficient(4, 2)
end
main()
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
/*
Scala Program for
Permutation coefficient program
*/
class Coefficient()
{
def permutationCoefficient(n: Int, k: Int): Unit = {
// Set initial value
var p: Int = 1;
var i: Int = n;
while (i >= (n - k + 1))
{
p = p * i;
i -= 1;
}
// Display calculated results
print(" ( n = " + n + ", k = " + k + " ) : " + p + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Coefficient = new Coefficient();
// Test samples
task.permutationCoefficient(7, 3);
task.permutationCoefficient(4, 2);
}
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
/*
Swift 4 Program for
Permutation coefficient program
*/
class Coefficient
{
func permutationCoefficient(_ n: Int, _ k: Int)
{
// Set initial value
var p: Int = 1;
var i: Int = n;
while (i >= (n - k + 1))
{
p = p * i;
i -= 1;
}
// Display calculated results
print(" ( n = ", n ,", k = ", k ," ) : ", p );
}
}
func main()
{
let task: Coefficient = Coefficient();
// Test samples
task.permutationCoefficient(7, 3);
task.permutationCoefficient(4, 2);
}
main();
Output
( n = 7 , k = 3 ) : 210
( n = 4 , k = 2 ) : 12
/*
Kotlin Program for
Permutation coefficient program
*/
class Coefficient
{
fun permutationCoefficient(n: Int, k: Int): Unit
{
// Set initial value
var p: Int = 1;
var i: Int = n;
while (i >= (n - k + 1))
{
p = p * i;
i -= 1;
}
// Display calculated results
print(" ( n = " + n + ", k = " + k + " ) : " + p + " \n");
}
}
fun main(args: Array < String > ): Unit
{
val task: Coefficient = Coefficient();
// Test samples
task.permutationCoefficient(7, 3);
task.permutationCoefficient(4, 2);
}
Output
( n = 7, k = 3 ) : 210
( n = 4, k = 2 ) : 12
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment