SlideShare a Scribd company logo
Recursion Examples:
1. Factorial
2. Binary representation
1. Calculating Factorial of a Number:
factorial function(n!): the product of the integers between 1 and n.
In other words multiply all whole numbers from our chosen number down to 1.
➢ Code:
Using Loop Using Recursion
int main()
{
int num;cin >> num;
int factorial = 1;
for (int i = 1; i <= num; ++i)factorial *=
i;
cout << factorial;
return 0;
}
int factorial(int num) {
if (num == 0)return 1;
else return num * factorial(num - 1);
}
int main()
{
int num;cin >> num;
cout << factorial(num);
return 0;
}
➢ Behind the scenes:
n!=n*(n-1)!
4! = 4 × 3 × 2 × 1 = 24
1! = 1
int factorial(int 3)
{
if (0 == 0)return 1;
else
return0*factorial(-
1);
}
num =0
int factorial(int 3)
{
if (1 == 0)return 1;
else
return1*factorial(0);
}
num =1
int factorial(int 3)
{
if (2 == 0)return 1;
else
return2*factorial(1);
}
num =2
int factorial(int 3)
{
if (3 == 0)return 1;
else
return3*factorial(2);
}
num=3
int factorial(int 4)
{
if (4 == 0)return 1;
else
return4*factorial(3);
}
num =4
Key :
Red: False -Not executed
Green: True - Executed
 Important Note:
when a function calls itself, a new copy of that function is run. The local variables in the second version are
independent of the local variables in the first, and they cannot affect one another directly.
➢ Steps:
1. Identify the basic cases (those in which the subprogram can solve the problem directly without recurring to
recursive calls) and determine how they are solved.
For example, in the case of factorial, the only basic case used in the function is n=0. Similarly, we could have
considered a more general basic case (e.g., n ≤ 1). In both cases, the function should return 1.
2. Determine how to resolve the non-basic cases in terms of the basic cases, which we assume we can already
solve.
In the case of a factorial, we know that the factorial of a number n greater than zero is n*factorial(n-1).
3. Make sure that the parameters of the call move closer to the basic cases at each recursive call. This should
guarantee a finite sequence of recursive calls that always terminates.
In the case of a factorial, n-1 is closer to 0 than n. Therefore, we can guarantee that this function terminates.
2. Writing Binary Representation of a Number:
➢ Code
Using Loop Using Recursion
int binary[32];
int main()
{
int num;cin >> num;
int i = 0;
for (; 0 < num; ++i) {
binary[i] = num % 2;
num /= 2;
}
for (int j = i - 1; j >= 0; j--) {
cout << binary[j];
}
return 0;
}
void binary(int num)
{
if (num == 1) cout << 1;
else {
binary(num / 2);
cout << num % 2;
}
}
int main()
{
int num; cin >> num;
binary(num);
return 0;
}
1
3*2=6
1*2=2
1*1=1
4*6=24
4!= 4*3!
3!=3*2!
2!=2*1!
1!=1*0!
0!=1

More Related Content

What's hot

Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
Pooja B S
 
Pointers lesson 4 (malloc and its use)
Pointers lesson 4 (malloc and its use)Pointers lesson 4 (malloc and its use)
Pointers lesson 4 (malloc and its use)
SetuMaheshwari1
 
R normal distribution
R   normal distributionR   normal distribution
R normal distribution
Learnbay Datascience
 
Builtin Functions and variables naming rules
Builtin Functions and variables naming rulesBuiltin Functions and variables naming rules
Builtin Functions and variables naming rules
Nigah Mathour
 
3.2 Derivative as a Function
3.2 Derivative as a Function3.2 Derivative as a Function
3.2 Derivative as a Functiongregcross22
 
Basic python programs
Basic python programsBasic python programs
Basic python programs
RaginiJain21
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10alish sha
 
Check box, image
Check box, imageCheck box, image
Check box, image
eli priyatna laidan
 
Pointer example
Pointer examplePointer example
Pointer example
University of Potsdam
 
Mac Interview Workshop 2021
Mac Interview Workshop 2021Mac Interview Workshop 2021
Mac Interview Workshop 2021
Michael Viveros
 
Matlab numbers
Matlab numbersMatlab numbers
Matlab numbers
pramodkumar1804
 

What's hot (11)

Python Conditionals and Functions
Python Conditionals and FunctionsPython Conditionals and Functions
Python Conditionals and Functions
 
Pointers lesson 4 (malloc and its use)
Pointers lesson 4 (malloc and its use)Pointers lesson 4 (malloc and its use)
Pointers lesson 4 (malloc and its use)
 
R normal distribution
R   normal distributionR   normal distribution
R normal distribution
 
Builtin Functions and variables naming rules
Builtin Functions and variables naming rulesBuiltin Functions and variables naming rules
Builtin Functions and variables naming rules
 
3.2 Derivative as a Function
3.2 Derivative as a Function3.2 Derivative as a Function
3.2 Derivative as a Function
 
Basic python programs
Basic python programsBasic python programs
Basic python programs
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
Check box, image
Check box, imageCheck box, image
Check box, image
 
Pointer example
Pointer examplePointer example
Pointer example
 
Mac Interview Workshop 2021
Mac Interview Workshop 2021Mac Interview Workshop 2021
Mac Interview Workshop 2021
 
Matlab numbers
Matlab numbersMatlab numbers
Matlab numbers
 

Similar to Recursion examples

Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
Flavia Tembo Kambale
 
Recurrence relationships
Recurrence relationshipsRecurrence relationships
Recurrence relationships
Devansh16
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
SSE_AndyLi
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
jvjfvvoa
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
Sreedhar Chowdam
 
1- please please please don't send me an incomplete program this code.docx
1- please please please don't send me an incomplete program this code.docx1- please please please don't send me an incomplete program this code.docx
1- please please please don't send me an incomplete program this code.docx
EvandWyBurgesss
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
PochupouOwo
 
algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
Nehagupta259541
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
AmayJaiswal4
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
KokilaK25
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
smruti sarangi
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
Gradeup
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
3.pdf
3.pdf3.pdf
3.pdf
AlaaOdeh18
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
21221
2122121221
21221
inKFUPM
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
Deepak John
 

Similar to Recursion examples (20)

Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Recurrence relationships
Recurrence relationshipsRecurrence relationships
Recurrence relationships
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
1- please please please don't send me an incomplete program this code.docx
1- please please please don't send me an incomplete program this code.docx1- please please please don't send me an incomplete program this code.docx
1- please please please don't send me an incomplete program this code.docx
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
 
algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
3.pdf
3.pdf3.pdf
3.pdf
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
21221
2122121221
21221
 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
 

Recently uploaded

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 

Recently uploaded (20)

Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 

Recursion examples

  • 1. Recursion Examples: 1. Factorial 2. Binary representation 1. Calculating Factorial of a Number: factorial function(n!): the product of the integers between 1 and n. In other words multiply all whole numbers from our chosen number down to 1. ➢ Code: Using Loop Using Recursion int main() { int num;cin >> num; int factorial = 1; for (int i = 1; i <= num; ++i)factorial *= i; cout << factorial; return 0; } int factorial(int num) { if (num == 0)return 1; else return num * factorial(num - 1); } int main() { int num;cin >> num; cout << factorial(num); return 0; } ➢ Behind the scenes: n!=n*(n-1)! 4! = 4 × 3 × 2 × 1 = 24 1! = 1 int factorial(int 3) { if (0 == 0)return 1; else return0*factorial(- 1); } num =0 int factorial(int 3) { if (1 == 0)return 1; else return1*factorial(0); } num =1 int factorial(int 3) { if (2 == 0)return 1; else return2*factorial(1); } num =2 int factorial(int 3) { if (3 == 0)return 1; else return3*factorial(2); } num=3 int factorial(int 4) { if (4 == 0)return 1; else return4*factorial(3); } num =4 Key : Red: False -Not executed Green: True - Executed
  • 2.  Important Note: when a function calls itself, a new copy of that function is run. The local variables in the second version are independent of the local variables in the first, and they cannot affect one another directly. ➢ Steps: 1. Identify the basic cases (those in which the subprogram can solve the problem directly without recurring to recursive calls) and determine how they are solved. For example, in the case of factorial, the only basic case used in the function is n=0. Similarly, we could have considered a more general basic case (e.g., n ≤ 1). In both cases, the function should return 1. 2. Determine how to resolve the non-basic cases in terms of the basic cases, which we assume we can already solve. In the case of a factorial, we know that the factorial of a number n greater than zero is n*factorial(n-1). 3. Make sure that the parameters of the call move closer to the basic cases at each recursive call. This should guarantee a finite sequence of recursive calls that always terminates. In the case of a factorial, n-1 is closer to 0 than n. Therefore, we can guarantee that this function terminates. 2. Writing Binary Representation of a Number: ➢ Code Using Loop Using Recursion int binary[32]; int main() { int num;cin >> num; int i = 0; for (; 0 < num; ++i) { binary[i] = num % 2; num /= 2; } for (int j = i - 1; j >= 0; j--) { cout << binary[j]; } return 0; } void binary(int num) { if (num == 1) cout << 1; else { binary(num / 2); cout << num % 2; } } int main() { int num; cin >> num; binary(num); return 0; } 1 3*2=6 1*2=2 1*1=1 4*6=24 4!= 4*3! 3!=3*2! 2!=2*1! 1!=1*0! 0!=1