Question

C++ //Write prototype for function factorial that accepts an int num and returns an int /*...

C++

//Write prototype for function factorial that accepts an int num and returns an int

/*

WITH LOOP OF YOUR CHOICE:

Write code for function factorial that accepts an int num

and returns the num's factorial

factorial(5);

1*2*3*4*5

returns 120

DON'T FORGET TO WRITE TEST CASE.

*/

//write includes statements

//write using statements for cin and cout


/*

Use a do while loop to prompt the user for

a number, call the factorial function, and display the number's

factorial. Also, loop continues as long as user wants to.

*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int factorial(int n);

int main() {
    int n;
    do {
        cout << "Enter a number(negative to stop): ";
        cin >> n;
        if (n >= 0) {
            cout << "Factorial of " << n << " is " << factorial(n) << endl;
        }
    } while (n >= 0);
    return 0;
}

int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

Add a comment
Know the answer?
Add Answer to:
C++ //Write prototype for function factorial that accepts an int num and returns an int /*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ clock.h /* Write a function get_hours that returns an int and accepts an int seconds_since_1970...

    C++ clock.h /* Write a function get_hours that returns an int and accepts an int seconds_since_1970 parameter */ int get_hours(int seconds_since_1970); /* Write a function get_minutes that returns an int and accepts an int seconds_since_1970 parameter */ int get_minutes(int seconds_since_1970); /* Write a function get_seconds that returns an int and accepts an int seconds_since_1970 parameter */ int get_seconds(int seconds_since_1970); clock.cpp #include "clock.h" /* Write get_hours code to return hours given seconds since 1970 int */ /* Write get_minutes code to...

  • C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial...

    C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial of a number. The factorial of a number is given by n! = 1*2* ... *n So 1! = 1, 2! = 1*2, 3! = 1*2*3 ... For example: Test Result printf("%llu\n", factorial(5)); 120 printf("%lu\n", factorial(20)); 2432902008176640000

  • C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa...

    C++ //get_letter_grade.h /* Write a function gpa_to_letter_grade that returns a string and accepts a double gpa parameter */ //get_letter_grade.cpp /* Write function code for gpa_to_letter_grade that returns a string and accepts a double gpa parameter YOU MUST USE A SWITCH STATEMENT Given a double 3.5 returns the string A TIP: You'll have to convert the double to an int using multiplication Table 3.5 to 4 returns an A 3.0 to 3.49 returns a B 1.7 to 2.99 returns a C...

  • Given the following function: int fun1(int count){ int Num ; for (i = 0; i <...

    Given the following function: int fun1(int count){ int Num ; for (i = 0; i < count; ++i) { cin >> Value; if (i == 0) Num = Value; else if (Value > Num) Num = Value; } return Num ; } What is the output of the following C++ code segment if the following list is entered? (Input list:  5 -15 -90 -2 -60 -30) int Num = 0 , Value, numValues; cin >> numValues; if (numValues > 0) cout...

  • In c++ code and test a function with prototype float except(int arg); that accepts an int...

    In c++ code and test a function with prototype float except(int arg); that accepts an int argument and returns the expression 1.0 / arg if arg != 0; otherwise, except() throws an exception of type string. A suitable message might be “Error – attempt to divide by zero”.

  • Consider the function prototype below. Write the missing function call. float Rent(float , int &); int...

    Consider the function prototype below. Write the missing function call. float Rent(float , int &); int main() { int d; // Days in hotel. float r=149.55; // Daily Rate (r). float cost; // Total cost of stay of d days. cout << "Daily room rate = $" << r << endl; cout << "#Days you will be staying: "; cin >> d; ______________________________________________ // MISSING statement. cout << "The cost of your stay of " << d << " days...

  • Help with these questions A function rectarea has already been defined with this prototype: int rectarea(int...

    Help with these questions A function rectarea has already been defined with this prototype: int rectarea(int width); It collects an integer height from the user and returns the area of the resulting triangle; Write code that will call rectarea twice with values 5 and 10, and print the results obtained. Warning: in a composed function like "f(a),b0);", you don't know whether a) will be called first or b(!. The variable age has been defined as an integer. Write code which...

  • In C++ 9) (5 pts) Complete the following function that takes 3 arguments of a circle...

    In C++ 9) (5 pts) Complete the following function that takes 3 arguments of a circle : - radius (input argument) - area (output argument, to be calculated) - circumference (output argument, to be calculated) All arguments are double type. If a radius is negative, the function returns false, otherwise it returns true. The function does only calculation, and does nothing else. Assume that all #include are already there                                     // Fill in your Function prototype bool circleAreaAndCircumference ( _______,...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT