1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Serjik [45]
3 years ago
10

C++ code-- Factorial Recursion

Computers and Technology
2 answers:
Aloiza [94]3 years ago
3 0
Handle the print outside of the factorial function, which only needs one userVal when called.

int factorial( int foo )
{
  if( foo == 1 )
   return 1;
 else
   return( foo * factorial( foo - 1 ) );
}
oksano4ka [1.4K]3 years ago
3 0

Answer:

void PrintFactorial(int factCounter, int factValue){

int nextCounter = 0;

int nextValue = 0;

if (factCounter == 0) { // Base case: 0! = 1

cout << "1" << endl;

}

else if (factCounter == 1) { // Base case: Print 1 and result

cout << factCounter << " = " << factValue << endl;

}

else { // Recursive case

cout << factCounter << " * ";

nextCounter = factCounter - 1;

nextValue = nextCounter * factValue;

printFactorial(nextCounter, nextValue);

}

}

Explanation:

The only things that is left is calling the recursive function in the else statement. It is the bolded command on the answer.

factCounter is the value we are multiplying. In the function, it is nextCounter. So for 5, it is 5, then 4, then 3, until 0...

factValue is the value of the factorial. In the function, it is nextValue So it is 5, then 5*4 = 20, then 5*4*3 = 60, then...

void PrintFactorial(int factCounter, int factValue){

int nextCounter = 0;

int nextValue = 0;

if (factCounter == 0) { // Base case: 0! = 1

cout << "1" << endl;

}

else if (factCounter == 1) { // Base case: Print 1 and result

cout << factCounter << " = " << factValue << endl;

}

else { // Recursive case

cout << factCounter << " * ";

nextCounter = factCounter - 1;

nextValue = nextCounter * factValue;

printFactorial(nextCounter, nextValue);

}

}

You might be interested in
The Open Systems Interconnection (OSI) is a conceptual model whose purpose is to make networks more manageable. Its objectives i
andrew-mc [135]

Answer:

A. data-link layer

Explanation:

Data-link layer is the first layer and allows a contiguous sequence of bits to be transmitted through the network.

On top of it is the Network Layer and it handles the task of routing network messages from one computer to another.

Then comes the Transport Layer which is the basic layer at which one network computer communicates with another network computer.

6 0
3 years ago
What type of user account should Tuan’s brother use?
Sauron [17]

Answer:

d so if he comes back any time soon

Explanation:

6 0
4 years ago
Salvado has a flash dish of is GB
exis [7]

Answer:

About 1,655 files or exactly 1655.646315789473684210526315785

1578

Source(s):

1.5 gb = 1,500 mb<---wrong right --->1536

Explanation:

6 0
3 years ago
What are the advantages and disadvantages of nuclear energy?
Vilka [71]
Advantages: Nuclear energy has the ability to produce electricity without greenhouse gas emissions. It produces electricity without pollution

Disadvantages: The nuclear weapons threats. Some reactors produce plutonium which can be used to make nuclear weapons
7 0
3 years ago
Explain the procedure you will undertake to create a new partition​
MA_775_DIABLO [31]

To create a partition from unpartitioned space follow these steps:

Right click This PC and select Manage.

Open Disk Management.

Select the disk from which you want to make a partition.

Right click the Un-partitioned space in the bottom pane and select New Simple Volume.

Enter the size and click next and you are done.

8 0
3 years ago
Other questions:
  • Given the integer variables x and y, write a fragment of code that assigns the larger of x and y to another integer variable max
    5·1 answer
  • (1) In Tamara's science class, the students are learning Which sentence shows an action that is extrinsically
    7·1 answer
  • David has created a lot of styles and now his Quick Style Gallery contains styles he no longer uses.
    14·2 answers
  • Many computer magazines and Web sites present comparisons of several DBMSs. Find one such DBMS comparison article and compare th
    10·1 answer
  • While doing online research you enter this keyword search with a truncation character: man* Which of the following would not be
    15·1 answer
  • PLEASE HELP FAST !!!!!!!!!!
    12·1 answer
  • Which of the following is a technique used by hackers to identify unsecured wireless network locations to other hackers?A. Blues
    10·1 answer
  • Answer for 5,6,7 any one know plz
    8·1 answer
  • 1.erros can easily be ____ 2.work is done in an ____ no mess environment 3.colors do not _____ and brushes are never ______ 4.st
    14·1 answer
  • Question 9
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!