-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcombination.C
More file actions
90 lines (78 loc) · 2.56 KB
/
combination.C
File metadata and controls
90 lines (78 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* @file combination.C
* @brief Calculates the combination (nCr) of two numbers.
*
* @author Amey Thakur
* GitHub: https://github.com/Amey-Thakur
* @author Mega Satish
* GitHub: https://github.com/msatmod
* @author Hasan Rizvi
* GitHub: https://github.com/rizvihasan
*
* @project QUADTREE-VISUALIZER
* @group Phi-CS-73
* @batch 2022
* @repo https://github.com/Amey-Thakur/QUADTREE-VISUALIZER
* @date 2021
* @license MIT
*
* Developed as part of the Phi Education Training (Milestone 2) and
* BE Major-Project @ Terna Engineering College, University of Mumbai.
*
* This program calculates the combination (nCr) using the formula:
* nCr = n! / (r! * (n-r)!)
* where n! denotes the factorial of n.
*
* @return int Returns 0 on successful execution.
*/
#include <conio.h> /* Console I/O library for clrscr() and getch() (DOS/Turbo C specific) */
#include <stdio.h> /* Standard I/O library for printf and scanf functions */
/**
* @brief Calculates the factorial of a given number.
* @param num The number to calculate factorial for.
* @return int The factorial of the input number.
*/
int factorial(int num);
/**
* @brief Main function - Entry point of the program.
*
* Prompts the user to enter values for n and r, validates the input,
* calculates the combination (nCr), and displays the result.
*/
int main() {
int n, r; /* n: total items; r: items to choose */
float nCr; /* Result of the combination calculation */
clrscr(); /* Clears the console screen (Turbo C specific) */
/* Prompt user to enter the value of n */
printf("Enter value of n in nCr\n");
scanf("%d", &n);
/* Prompt user to enter the value of r */
printf("Enter value of r in nCr\n");
scanf("%d", &r);
/* Validate input: n >= r and both n, r >= 0 */
if (n >= r && n >= 0 && r >= 0) {
/* Calculate nCr = n! / (r! * (n-r)!) */
nCr = (float)factorial(n) / (factorial(r) * factorial(n - r));
printf("Value of %dC%d=%f\n", n, r, nCr);
} else {
printf("Invalid Entry\n");
}
getch(); /* Wait for a key press before closing (Turbo C specific) */
return 0; /* Return 0 to indicate successful execution */
}
/**
* @brief Calculates the factorial of a given number.
*
* Uses an iterative approach to compute n! = 1 * 2 * 3 * ... * n.
*
* @param num The number to calculate factorial for.
* @return int The factorial of the input number.
*/
int factorial(int num) {
int i, fact = 1;
/* Multiply all integers from 1 to num */
for (i = 1; i <= num; i++) {
fact = fact * i;
}
return fact;
}