{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

factfind - else If the integer is less than zero entry is...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// factfind.cpp // A program that calculates the factorial of an integer recursively. / #include<iostream.h> // necessary for stream I/O # // function prototype long factorial(long n); l int main() { long n; // number entered by user long result; // result returned by factorial function cout << "Enter a non-negative integer: "; cin >> n; if(n == 0) // If the integer is 0, print message and do no calculation. { cout << "By definition, the factorial of 0 is 1.\n"; } else { if(n > 0) // If the integer is greater than zero, calculate { // the factorial. result = factorial(n); // Call the recursive function. cout << "The factorial of " << n << " is " << result << ".\n";
Image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } else // If the integer is less than zero, entry is not valid. { cout << "Not a valid integer.\n"; } } return 0; } // Recursive function that calculates factorial. long factorial(long n) { long fact; // Local variable returned by function. if(n > 1) { // If n is not one, make another recursive call. fact = n * factorial(n - 1); } else { // Exit condition fact = 1; // If n is one, recursion stops and flow of logic } // begins "backing out" of recursive calls. return(fact); // Return fact to the next level of recursion. }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern