Are you ready to dive into the world of C++ programming? In this article, we will explore some of the fundamental yet crucial C++ programs that are based on the principles of Discrete Mathematics and Numerical Methods. By the end of this article, you will have a solid understanding of how to apply the concepts of Discrete Mathematics and Numerical Methods in C++ programming. So, buckle up, and let’s get started!

**Related: **See our guide on Simple C Programs Based on Discrete Mathematics for more details.

## 11. Write a program to solve the equation nPr=n!/(n-r)!

In Discrete Mathematics, **nPr**, also known as a **permutation** of **n** things taken **r** at a time, is a mathematical formula used to calculate the number of ways to arrange **r** distinct items from a set of **n** distinct items. It can be represented as **P(n, r)** or **nPr**.

The formula for nPr is **n!/(n-r)!** where n! is the factorial of n, and (n-r)! is the factorial of (n-r). Factorials are the product of all positive integers less than or equal to the number.

To give an example, if we want to find the number of ways to arrange 3 books from a set of 5 books, we would use the formula nPr = 5P3 = 5! / (5-3)! = 5! / 2! = 120 / 2 = 60.

It’s important to note that the **order of the elements is important in permutation**. It’s different than a combination, in which the order of the elements doesn’t matter. Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Enter the value of n: 5
Enter the value of r: 3
The value of Permutation is: 60
```

## 12. Write a program to solve the equation nCr=n!/r!(n-r)!

In Discrete Mathematics, **nCr**, also known as a **combination** of **n** things taken **r** at a time, is a mathematical formula used to calculate the number of ways to select **r** items from a set of **n** items without regard to the order of the items. It can be represented as **C(n, r)** or **nCr.**

The formula for nCr is **n! / (r! * (n-r)!)** where n! is the factorial of n, r! is the factorial of r, and (n-r)! is the factorial of (n-r). Factorials are the product of all positive integers less than or equal to the number.

For example, if we want to find the number of ways to select 3 books from a set of 5 books, we would use the formula nCr = 5C3 = 5! / (3! * (5-3)!) = 5! / (3! * 2!) = 10.

It’s important to note that the **order of the elements doesn’t matter in combination**, unlike permutation. Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Enter value of n: 5
Enter value of r: 3
The combination of given number is: 10
```

## 13. Program to find x^n using recursion.

Here is how the output of the program will appear when you run it:

```
Enter the value x: 2
Enter the value of n: 8
The value of 2^8 = 256
```

**Related:** See our guide on C Programs for Practice: Learn Simple C Programs for Beginners for more details.

## 14. Write a C++ program to sort a given array of range 0 to 10 using the Pigeonhole Principle.

In Discrete Mathematics, the **pigeonhole principle** is a statement about the number of items that can be placed into **pigeonholes** without having at least one pigeonhole with more than one item. The principle states that if **n items** are placed into **m pigeonholes**, with **n > m**, then at least one pigeonhole must contain more than one item.

The pigeonhole principle is often used to prove that certain operations or algorithms cannot be done in a certain number of steps. It can also be used to find the minimum number of elements that must be removed from a set to ensure that no two elements are the same.

It’s a simple yet powerful principle in Discrete Mathematics and is widely used in combinatorics, graph theory, computer science, and other fields. Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Enter length of array: 5
Enter element of array from range (0-10): 1 2 2 1 3
Sorted Array: 1 1 2 2 3
```

## 15. Write a C++ program to find the Union of a given sorted array set.

In set theory, the union of two sets is a new set that contains all the elements from both of the original sets. The symbol used to denote the union of two sets is ∪. For example, if A and B are two sets, then the union of A and B is represented as **A ∪ B**. The resulting set will contain all the elements that are present in either set A or set B or in both sets.

For example, if set A contains {1, 2, 3} and set B contains {3, 4, 5}, then the union of set A and set B is {1, 2, 3, 4, 5}.

It’s important to note that in set theory, the elements in a set are unique, thus there are no repeated elements in the resulting set.

Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Set A: {1, 2, 5, 7, 9, 11, 12, 14, 15, 18}
Set B: {0, 2, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 18, 19}
Set A U B: {0, 1, 2, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15, 16, 18, 19}
```

## 16. Write a C++ program to find the Intersection of a given sorted array set.

In set theory, the intersection of two sets is a new set that contains only the elements that are present in both of the original sets. The symbol used to denote the intersection of two sets is ∩. For example, if A and B are two sets, then the intersection of A and B is represented as **A ∩ B**. The resulting set will contain only the elements that are present in both set A and set B.

For example, if set A contains {1, 2, 3} and set B contains {2, 3, 4}, then the intersection of set A and set B is {2, 3}.

Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Set A: {1, 2, 5, 7, 9, 11, 12, 14, 15, 18}
Set B: {0, 2, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 18, 19}
Set A Intersection B: {2, 5, 7, 11, 12, 14, 15, 18}
```

## 17. Write a C++ program to find the Difference of a given sorted array set.

In set theory, the difference between two sets is a new set that contains only the elements that are present in the first set but not in the second set. The symbol used to denote the difference between two sets is – or /. For example, if A and B are two sets, then the difference of A and B is represented as** A – B or A \ B**. The resulting set will contain only the elements that are present in set A but not in set B.

For example, if set A contains {1, 2, 3} and set B contains {2, 3, 4}, then the difference between set A and set B is {1}.

Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Set A: {1, 2, 5, 7, 9, 11, 12, 14, 15, 18}
Set B: {0, 2, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 18, 19}
Set A - B: {1, 9}
```

## 18. Write a C++ program to find the Symmetric Difference of a given sorted array set.

In set theory, the symmetric difference between two sets is a new set that contains only the elements that are present in one set but not in the other set. The symbol used to denote the symmetric difference between two sets is ⊕. For example, if A and B are two sets, then the symmetric difference between A and B is represented as **A ⊕ B**. The resulting set will contain only the elements that are present in either set A or set B, but not in both sets.

For example, if set A contains {1, 2, 3} and set B contains {2, 3, 4}, then the symmetric difference between set A and set B is {1, 4}.

Let’s implement this with the help of the below C++ program.

Here is how the output of the program will appear when you run it:

```
Set A: {1, 2, 5, 7, 9, 11, 12, 14, 15, 18}
Set B: {0, 2, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 18, 19}
Set A + B: {0, 1, 4, 6, 9, 13, 16}
```

Vilashkumar is a Python developer with expertise in Django, Flask, API development, and API Integration. He builds web applications and works as a freelance developer. He is also an automation script/bot developer building scripts in Python, VBA, and JavaScript.