Vous êtes sur la page 1sur 39

,

-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! = 1*2*3**(N-1)*N = N*(N-1)**3*2*1 = N*(N-1)!

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

5! 5*4! = 5*24 = 120



1
#include <iostream>
using namespace std;
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout <<"Eyreg too oruul";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial" << n << " = " << factorial;
return 0;
}

:

Eyreg too oruul 15


Factorial:15 = 2004310016
--------------------------------
Process exited after 2.163 seconds with return value 0
Press any key to continue . . .

2
#include<iostream>
using namespace std;
int factorial(int n);
int main()
{ int n;
cout << "Eyreg too oruul ";
cin >> n;
cout << "Factorial:" << n << " = " << factorial(n);
return 0; }
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1; }

:

Eyreg too oruul 10


Factorial:10 = 3628800
--------------------------------
Process exited after 0.7898 seconds with return value 0
Press any key to continue . . .

C++ ?
void recurse()
{
... .. ...
recurse();
... .. ...
}

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;

cout << "Eyreg too oruul ";


cin >> n;

for (int i = 1; i <= n; ++i) {


sum += i;
}
cout << "Niilber ni= " << sum;
return 0;
}

:

Eyreg too oruul 100


Niilber ni= 5050
--------------------------------
Process exited after 2.654 seconds with return value 0
Press any key to continue . . .

N 2
#include<iostream>
using namespace std;
int add(int n);
int main()
{ int n;
cout << "Eyreg too oruul: ";
cin >> n;
cout << "Niilber = " << add(n);
return 0;
}
int add(int n)
{ if(n != 0)
return n + add(n - 1);
return 0;
}

:

Eyreg too oruul: 50


Niilber = 1275
--------------------------------
Process exited after 3.402 seconds with return value 0
Press any key to continue . . .

1
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "Hoer too oruul: ";
cin >> n1 >> n2;

while(n1 != n2)
{ if(n1 > n2)
n1 -= n2;
else
n2 -= n1; }
cout << "Hamgiin ih huvaagch ni" << n1;
return 0; }

:

Hoer too oruul: 100


90
Hamgiin ih huvaagch ni10
--------------------------------
Process exited after 2.556 seconds with return value 0
Press any key to continue . . .

2
#include <iostream>
using namespace std;
int hcf(int n1, int n2);
int main()
{ int n1, n2;
cout << "Eyreg hoer too oruul ";
cin >> n1 >> n2;
cout << "Hamgiin ih huvaagch: " << n1 << " ba " << n2 << " ni " << hcf(n1, n2);
return 0; }

int hcf(int n1, int n2)


{ if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1; }

:

Eyreg hoer too oruul 100


65
Hamgiin ih huvaagch: 100 ba 65 ni 5
--------------------------------
Process exited after 5.545 seconds with return value 0
Press any key to continue . . .
.
#DEFINE
#DEFINE

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 < > < >

.

\ .
..
.

#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
#define PRINT(name, age) \
cout << name << - << age

void main()
{

.....

PRINT(, 25);
PRINT(, 28);

.....

}
#DEFINE
.

#define MUL2(num) 2*num

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 .
.

#define MUL2(num) 2 * (num)

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

Vous aimerez peut-être aussi

  • Lecture 6 2018
    Lecture 6 2018
    Document50 pages
    Lecture 6 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 11
    Lecture 11
    Document24 pages
    Lecture 11
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lec1-1 Corporate Activity Shine
    Lec1-1 Corporate Activity Shine
    Document61 pages
    Lec1-1 Corporate Activity Shine
    Amarsaikhan Tuvshinbayar
    0% (1)
  • Lecture 15
    Lecture 15
    Document40 pages
    Lecture 15
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 9
    Lecture 9
    Document46 pages
    Lecture 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 12
    Lecture 12
    Document34 pages
    Lecture 12
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 4 2018
    Lecture 4 2018
    Document47 pages
    Lecture 4 2018
    Amarsaikhan Tuvshinbayar
    100% (4)
  • Lecture - 10
    Lecture - 10
    Document35 pages
    Lecture - 10
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 13
    Lecture 13
    Document46 pages
    Lecture 13
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 7 20108
    Lecture 7 20108
    Document47 pages
    Lecture 7 20108
    Amarsaikhan Tuvshinbayar
    75% (4)
  • Lecture 8 2018
    Lecture 8 2018
    Document54 pages
    Lecture 8 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 3 2018
    Lecture 3 2018
    Document42 pages
    Lecture 3 2018
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 5 2018
    Lecture 5 2018
    Document54 pages
    Lecture 5 2018
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 1 2018
    Lecture 1 2018
    Document39 pages
    Lecture 1 2018
    Amarsaikhan Tuvshinbayar
    100% (6)
  • IT101 Lecture 4
    IT101 Lecture 4
    Document72 pages
    IT101 Lecture 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 2 2018
    Lecture 2 2018
    Document33 pages
    Lecture 2 2018
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 8
    Programming Lec 8
    Document32 pages
    Programming Lec 8
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Lecture 2
    Lecture 2
    Document80 pages
    Lecture 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 4
    Programming Lec 4
    Document39 pages
    Programming Lec 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 1
    Lecture 1
    Document54 pages
    Lecture 1
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Programming Lec 7
    Programming Lec 7
    Document57 pages
    Programming Lec 7
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm 2
    Koosen 2018 Algorithm 2
    Document48 pages
    Koosen 2018 Algorithm 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 9
    Programming Lec 9
    Document39 pages
    Programming Lec 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • PL - 3
    PL - 3
    Document47 pages
    PL - 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm Programming 3
    Koosen 2018 Algorithm Programming 3
    Document46 pages
    Koosen 2018 Algorithm Programming 3
    Amarsaikhan Tuvshinbayar
    100% (1)