Académique Documents
Professionnel Documents
Culture Documents
-9 ,
: . //
.
.
www.emust.edu.mn
.
. ,
.
.
, .
. 1- N
N- . , 1- 1, 2-
2, 3- 6, 7- 5040 . 0-
1 . N! .
1! = 1
2! = 1*2
3! = 1*2*3
10! = 1*2*3*4*5*6*7*8*9*10
N! = 1*2*3**(N-1)*N
N- (N-1)- N-
. .
N- (N-1)- .
(N-1)- (N-2)-
.
1- 1 .
.
5! 5*4!
4! 4*3!
3! 3*2!
2! 2*1!
1! 1
, .
.
1! 1
2! 2*1! = 2*1 = 2
3! 3*2! = 3*2 = 6
4! 4*3! = 4*6 = 24
int main()
{
... .. ...
recurse();
... .. ...
}
Suppose the user entered 4, which is passed to the factorial() function.
1.In the first factorial() function, test expression inside if statement is
true. The return num*factorial(num-1); statement is executed, which
calls the second factorial() function and argument passed is num-
1which is 3.
2.In the second factorial() function, test expression inside if statement
is true. The return num*factorial(num-1); statement is executed, which
calls the third factorial()function and argument passed is num-1 which
is 2.
3.In the third factorial() function, test expression inside if statement is
true. The return num*factorial(num-1); statement is executed, which
calls the fourth factorial() function and argument passed is num-
1 which is 1.
4.In the fourth factorial() function, test expression inside if statement
is false. The return 1; statement is executed, which returns 1 to
third factorial() function.
5.The third factorial() function returns 2 to the
second factorial() function.
6.The second factorial() function returns 6 to the
first factorial() function.
7.Finally, the first factorial() function returns 24 to the main() function,
which is displayed on the screen.
main fact (1- ) 5 5 * fact(4)
fact (1- ) fact (2- ) 4 4 * fact(3)
fact (2- ) fact (3- ) 3 3 * fact(2)
fact (3- ) fact (4- ) 2 2 * fact(1)
fact (4- ) fact (5- ) 1 1
main fact 5 .
1- 5*fact(4) .
fact 4 .
1- . 4*fact(3)
fact 3 . fact 1
1 4
fact . 2*1 2 3 fact
, 3*2=6 2 fact , 4*6=24 1 fact
, 5*24=120 main .
. ,
.
, .
(num<=1) .
, .
.
.
if (num<=1)
return (1);
else
return (num*fact(num)); // !!!
(num<=1)
.
N 1
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
while(n1 != n2)
{ if(n1 > n2)
n1 -= n2;
else
n2 -= n1; }
cout << "Hamgiin ih huvaagch ni" << n1;
return 0; }
:
C++-
. .
++
= == . = ,
==
. .
#DEFINE
#define EQUALS ==
#define GETS_VALUE_OF =
x=y
x GETS_VALUE_OF y;
if (x==y)
if (x EQUALS y)
.
#DEFINE
#define .
.
\ .
..
.
#define GETS_VALUE_OF \
,
.
.
. #define- .
.
#DEFINE
#define EQUALS ==
#define EQ EQUALS
.
.
#define NINE 9
#define
.
..
, .
, EQUALS
== .
#DEFINE
#define .
.
.
.
#DEFINE
#define PRESS_KEY \
cout <<
void main()
{
.....
PRESS_KEY;
.....
}
#DEFINE
\ .
.
.
.
++- .
. ,
.
#DEFINE
#define PRINT(name, age) \
cout << name << - << age
void main()
{
.....
PRINT(, 25);
PRINT(, 28);
.....
}
#DEFINE
.
void main()
{
int a1,a2;
a1=MUL2(5);
a2=MUL2(2+3);
}
#DEFINE
1- 10 , 2-
10 7 . 2*2+3=7 .
MUL2(2+3) 2*2+3
.
. 7 .
.
2*2+3 , 2*(2+3)
, 10 .
6, 7, 8
, ,
.
.
.
, .
. ,
.
.
.
.
6, 7, 8, 9
.
.
.
,
.
.
6, 7, 8
.
, .
.
.
.
.
.
,
.
6, 7, 8
. #define .
. ,
.
1. ,
2. , 4- , -, ,
, , , ,
3. U.CS101
4. he2must.blogspot.com
5. https://www.programiz.com/cpp-programming/examples/factorial
6. https://www.programiz.com/cpp-programming/examples/factorial-recursion
7. https://www.programiz.com/cpp-programming/examples/sum-natural-number
8. https://www.programiz.com/cpp-programming/examples/natural-number-sum-recursion
9. https://www.programiz.com/cpp-programming/examples/hcf-gcd
10. https://www.programiz.com/cpp-programming/examples/hcf-recursion
11. https://www.programiz.com/cpp-programming/recursion