-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.cpp
269 lines (216 loc) · 8.05 KB
/
func.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// What is a Function?
// A function is a block of code that performs a specific task. You define a function once, and you can "call" or "invoke" it multiple times
// throughout your program.
// Components of a Function
// Return Type: The data type of the value that the function returns.
// It could be any valid C++ data type like int, float, char, void, etc. If the function does not return a value,
// its return type should be void.
// Function Name: A unique identifier for the function. You use this name when calling the function.
// Parameters: A function can take input values (called arguments or parameters). These are passed to the function when it is called.
// Function Body: The block of code enclosed in curly braces {} that defines what the function does.
// Return Statement: (Optional) If the function returns a value, the return statement sends that value back to the calling code.
// ***Syntax of a Function
// return_type function_name(parameter1_type parameter1, parameter2_type parameter2, ...) {
// // Code inside the function
// return value; // Optional, based on the return type
// }
// How Functions Work:
// Function Declaration (Prototype): A declaration tells the compiler about a function's name, return type, and parameters.
// It's like a promise that the function will exist somewhere in the code.
// Function Definition: This is where you write the actual code of the function.
// Function Call: Once you define a function, you can call it from the main() function or any other part of your code.
// #include <iostream>
// using namespace std;
// // Function declaration
// int add(int a, int b);
// // Main function
// int main() {
// int num1, num2, sum;
// // Asking user to input two numbers
// cout << "Enter two numbers: ";
// cin >> num1 >> num2;
// // Calling the add function and storing the result in sum
// sum = add(num1, num2);
// // Printing the result
// cout << "Sum = " << sum << endl;
// return 0;
// }
// // Function definition
// int add(int a, int b) {
// return a + b; // Return the sum of a and b
// }
// Function Declaration: int add(int a, int b); tells the compiler that the function add will take two integers as parameters and return an integer.
// Function Definition: int add(int a, int b) { return a + b; } is the actual code of the function. It takes two integers, adds them, and returns the result.
// Function Call: sum = add(num1, num2); calls the function add with the values num1 and num2. The result is stored in sum.
// Types of Functions:
// Void Functions: A function that doesn’t return any value. Its return type is void.
// void greet() {
// cout << "Hello, welcome to C++!" << endl;
// }
// Functions with Parameters: Functions that take input arguments (parameters) when called.
// void display(int number) {
// cout << "The number is: " << number << endl;
// }
// Functions with Return Values: Functions that return a value to the calling function.
// int square(int n) {
// return n * n;
// }
// Function Overloading
// Function overloading allows you to define multiple functions with the same name but different types or numbers of parameters.
// The compiler determines which function to call based on the number and types of arguments passed during the function call.
// Example of Function Overloading:
// #include <iostream>
// using namespace std;
// // Overloaded function to add two integers
// int add(int a, int b) {
// return a + b;
// }
// // Overloaded function to add two floating-point numbers
// float add(float a, float b) {
// return a + b;
// }
// // Overloaded function to add three integers
// int add(int a, int b, int c) {
// return a + b + c;
// }
// int main() {
// int int1 = 5, int2 = 10, int3 = 15;
// float float1 = 3.5, float2 = 2.3;
// // Calling the overloaded add functions
// cout << "Sum of two integers: " << add(int1, int2) << endl; // Calls int add(int, int)
// cout << "Sum of two floats: " << add(float1, float2) << endl; // Calls float add(float, float)
// cout << "Sum of three integers: " << add(int1, int2, int3) << endl; // Calls int add(int, int, int)
// return 0;
// }
// The function add is defined three times, each with different types or numbers of parameters.
// The function to be called is determined based on the number and types of arguments passed.
// add(int, int) adds two integers.
// add(float, float) adds two floats.
// add(int, int, int) adds three integers.
// Output:
// mathematica
// Sum of two integers: 15
// Sum of two floats: 5.8
// Sum of three integers: 30
// 2. Default Arguments
// Default arguments allow you to specify default values for function parameters. If no value is provided for a parameter
// when calling the function, the default value is used.
// Example of Default Arguments:
// #include <iostream>
// using namespace std;
// // Function with default arguments
// void display(int a = 10, int b = 20) {
// cout << "a = " << a << ", b = " << b << endl;
// }
// int main() {
// // Calling display with no arguments, uses default values
// display(); // Output: a = 10, b = 20
// // Calling display with one argument, overrides the default for 'a'
// display(100); // Output: a = 100, b = 20
// // Calling display with two arguments, overrides both default values
// display(100, 200); // Output: a = 100, b = 200
// return 0;
// }
// The function display has default values for a and b (a = 10, b = 20).
// If no values are provided when calling display(), the default values are used.
// If one or both values are provided, the defaults are overridden.
// Output:
// a = 10, b = 20
// a = 100, b = 20
// a = 100, b = 200
// Function Overloading is useful when you need the same function name for different parameter types or numbers, which keeps code
// organized and clean.
// Default Arguments allow for flexible function calls, providing default behavior while still allowing the caller to customize the
// function's behavior.
// #include <iostream>
// using namespace std;
// int sum(int a,int b){
// int d = a+b;
// return d;
// }
// int main(){
// cout <<sum(3,5)<<endl;
// return 0;
// }
// #include <iostream>
// using namespace std;
// float sum(float a,float b){//use double
// float d = a+b;
// return d;
// }
// double min(double c,double d){//parameters
// double m =c-d;
// return m;
// }
// int main(){
// // cout <<sum(3,5)<<endl;
// // return 0;
// cout <<min(9.99,4.89)<<endl;//arguments
// return 0;
// }
//SUM1-N
// #include <iostream>
// using namespace std;
// int sumN(int n){
// int sum =0;
// for(int i=0; i<=n;i++){
// sum += i;
// }
// return sum;
// }
// int main(){
// cout <<sumN(5)<<endl;
// return 0;
// }
//CALCULATE N FACTORIAL
// #include <iostream>
// using namespace std;
// int factN(int n){
// int fact =1;
// for(int i=1;i<=n;i++){
// // fact=fact*i;
// fact *=i;
// }
// return fact;
// }
// int main(){
// cout <<factN(5)<<endl;
// return 0;
// }
//SUM OF A DIGIT OF A NUM
// #include <iostream>
// using namespace std;
// int sumN(int num){
// int digSum =0;
// while(num >0){
// int lastdigit =num%10;
// num/=10;
// digSum +=lastdigit;
// }
// return digSum;
// }
// int main(){
// cout <<sumN(532)<<endl;
// return 0;
// }
//CALCULATE nCr biomial coefficient for n&r
#include <iostream>
using namespace std;
int factroial(int n){
int fact =1;
for (int i=1;i<=n;i++){
fact *=i;
}
return fact;
}
int ncr(int n,int r){
int fact_n =factroial(n);
int fact_r =factroial(r);
int fact_nmr =factroial(n-r);
return fact_n /(fact_r *fact_nmr);
}
int main(){
int n=8,r=2;
cout <<ncr(n,r)<<endl;
return 0;
}