Simple C Programs Based on Discrete Mathematics

Simple C Programs Based on Discrete Mathematics

In this article, let’s learn some of the basic but essential C programs based on Discrete Mathematics.

1. Write a program to find the greatest common divisor of two positive integers.

The greatest common divisor (GCD) of two or more numbers, which are not all zero, is the largest positive integer that divides each of the numbers. Below is the C source code to find the GCD of two numbers.

When you execute the above program, the output will look like this as shown below:

Enter two integers: 20 30
GCD of 20 and 30 is 10

2. Write a program to check whether the given number is prime or not.

A number that is divisible only by itself and 1 is called a Prime number. For example, 2, 3, 5, 7, 11,… Let’s implement this with the help of the below C program.

When you execute the above program, the output will look like this as shown below:

Enter any number to check: 41
Given number is prime number.

3. Write a program to find the factorial of a given number using a function (recursion).

The Factorial of a positive integer (number) is the sum of the multiplication of all the integers smaller than that positive integer denoted by “!”. For example, the factorial of 4 is 4 * 3 * 2 * 1 which equals 24. Let’s implement it in a C program with a recursive function.

When you execute the above program, the output will look like this as shown below:

Enter any number: 5
Factorial of 5 is: 120

4. Write a program to find the number of n objects taken r at a time nCr = n!/r!(n-r)!

The combination formula is used to find the number of possible combinations obtained by taking a sample of items from a larger set. In simple words, it shows how many different possible subsets can be made from the larger set where the order of the items chosen in the subset does not matter. The combination formula is:

combination-formula

Let’s implement the above formula in C to find out the combination of given numbers.

When you execute the above program, the output will look like this as shown below:

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

5. Write a program to find the least common multiple (LCM) of two positive integers.

The Least Common Multiple (LCM) of any two is the value that is evenly divisible by the two given numbers. Below is the C source code to find the LCM of two numbers.

When you execute the above program, the output will look like this as shown below:

Enter the value of a: 62
Enter the value of b: 42
The LCM of 62 and 42 is 1302.

6. Write a program to print the truth table for conjunction.

When we add two statements with the connector AND symbol, then it is known as a conjunction. The symbol for conjunction is ‘∧’ which can be read as ‘and’. When two statements A and B are joined in a statement, the conjunction is represented as A ∧ B. Below is the C source code to show the truth table for conjunction.

When you execute the above program, the output will look like this as shown below:

CONJUNCTION

| A | B | A^B |
_______________
| 0 | 0 | 0   |
_______________
| 0 | 1 | 0   |
_______________
| 1 | 0 | 0   |
_______________
| 1 | 1 | 1   |
_______________

7. Write a program to print the truth table for disjunction.

When we add two statements with the connector OR symbol, then it is known as disjunction. The symbol for disjunction is ‘V’ which can be read as ‘or’. When two statements A and B are joined in a statement, the disjunction is represented as A V B. Below is the C source code to show the truth table for disjunction.

When you execute the above program, the output will look like this as shown below:

DISJUNCTION

| A | B | AVB |
_______________
| 0 | 0 | 0   |
_______________
| 0 | 1 | 1   |
_______________
| 1 | 0 | 1   |
_______________
| 1 | 1 | 1   |
_______________

8. Write a program to print the truth table for negation.

When we add a statement with the connector NOT symbol, then it is known as negation. The symbol for negation is ‘~’ which can be read as ‘not’. For statement, A the negation is represented as ~A. Below is the C source code to show the truth table for negation.

When you execute the above program, the output will look like this as shown below:

NEGATION

| A | ~A |
__________
| 0 | 1  |
__________
| 1 | 0  |
__________

9. Write a program to verify equation a(b+c) = ab + ac.

The given equation can be verified by the truth table. Let’s implement it in a C program.

When you execute the above program, the output will look like this as shown below:

--------------------------------
| a | b | c | a(b+c) | (ab+ac) |
--------------------------------
| 0 | 0 | 0 | 0      | 0       |
| 0 | 0 | 1 | 0      | 0       |
| 0 | 1 | 0 | 0      | 0       |
| 0 | 1 | 1 | 0      | 0       |
| 1 | 0 | 0 | 0      | 0       |
| 1 | 0 | 1 | 1      | 1       |
| 1 | 1 | 0 | 1      | 1       |
| 1 | 1 | 1 | 1      | 1       |
--------------------------------

10. Write a program to print the Fibonacci series for given N terms.

The Fibonacci series (also called Fibonacci numbers) is the sum of the two preceding numbers. The sequence commonly starts from 0 and 1. A Fibonacci series can be given as 0, 1, 1, 2, 3, 5, 8, 13, … Let’s implement the Fibonacci series with the help of the below C program.

When you execute the above program, the output will look like this as shown below:

Enter number of terms you want to print: 8
0  1  1  2  3  5  8  13

Related:

C Programs for Practice: Learn Simple C Programs for Beginners
Simple C Programs Based on the Mathematical Statistics

Scroll to Top