0% found this document useful (0 votes)
26 views1 page

Lab 2 Question 3

This C++ program defines a function called factorial that takes an integer parameter n and returns the factorial of n. It initializes a variable fact to 1 and uses a for loop to multiply fact by each integer from 1 to n, storing the resulting product back in fact. The program returns the final value of fact, which will be the factorial of the original integer n passed to the function.

Uploaded by

Sam Khor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views1 page

Lab 2 Question 3

This C++ program defines a function called factorial that takes an integer parameter n and returns the factorial of n. It initializes a variable fact to 1 and uses a for loop to multiply fact by each integer from 1 to n, storing the resulting product back in fact. The program returns the final value of fact, which will be the factorial of the original integer n passed to the function.

Uploaded by

Sam Khor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

int main()

{
long factorial(int n)}

{
int counter;
long fact = 1;

//for Loop Block


for (int counter = 1; counter <= n; counter++)
{
fact = fact * counter;
}

return fact;
}

You might also like