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
2.<br> The force of impact is
NNADVOKAT [17]

Answer:

lực ( Tiếng Anh : force ) là bất kỳ ảnh hưởng nào làm một vật thể chịu sự thay đổi, hoặc là ảnh hưởng đến chuyển động, hướng của nó hay cấu trúc hình học của nó.

Explanation:

7 0
3 years ago
Label the parts of the plated salad.
sesenic [268]

Answer:

   3. Base (<em>under line</em>): usually a layer of salad greens that line the plate or bowl in which the salad will be served.

   2. Body (<em>main part</em>): consists of the main ingredients.

   1. Garnish: enhances the appearance while also complementing the overall taste; must be edible.

Dressing: liquid/ semi-liquid added to the body.

Explanation:

3 0
2 years ago
Whats the flow in this code, and whats the risk that its creating and what can i do to fix it? (c language)
Advocard [28]

Answer:

The risk is a buffer overflow.

Explanation:

Whatever the user passes as a command line argument, will be copied into the buffer. If the user passes more than 499 characters, the end of the buffer will be overwritten.

To solve it, compare the string length of argv[1] to 500 before copying, or even better, start using the new strcpy_s( ) function.

6 0
3 years ago
Why is IP configuration used with its components for network, broadcast, host addresses, and local host?
matrenka [14]

Answer:

GeoLocation

Explanation:

It is based on geolocation and wireless provider so it differs based on where and what hope this helps!

3 0
3 years ago
"in program arrays12.java a swap method is called. does the swap method exchange parameter values x and y?"
Artist 52 [7]

Answer:

The answer is "True".

Explanation:

In the given question option is missing, that is "true or false", which can be described as follows:

In the given question, it is declared, that an "arrays12.java" program file is declared So, the class name is  "arrays12" because in java class name and the program file name is the same.

  • Inside the class, a swap method is called, that first accepts value.
  • After accepting the value it is defined as another variable, that swap variable values, that's why the answer to this question is true.
7 0
4 years ago
Other questions:
  • A college meal plan allows students to:
    7·2 answers
  • How will computing change our world?
    8·1 answer
  • Where does communication take place?
    12·1 answer
  • The Analysis phase of the SDLC examines the event or plan that initiates the process and specifies the objectives, constraints,
    10·1 answer
  • Which one of the following items would you be most likely to keep in a database? A. Payroll records B. Address book C. Financial
    7·1 answer
  • 1. If your motherboard supports DIMM memory, will RIMM memory still work<br>on the board?​
    13·1 answer
  • When you use information hiding by selecting which properties and methods of a class to make public, you are assured that your d
    14·1 answer
  • Reverse Word Order: Write a program that reverses the order of the words in a given sentence. This program requires reversing th
    15·1 answer
  • Core to resource management system is the _________that coordinates the server hardware.
    8·1 answer
  • What is precipitation ????
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!