Uploaded by Joshua Kachingwe

ASSIGNMENT 6[1] B

advertisement
#include <iostream>
using namespace std;
//Creating the factorial function using the logic of f(n)= n!
long long factorial(int n)
value.
// function the returns a long long
// the function is called factorial
and it takes an integer as an argument
{
long long fac = 1;
// starting our function from 1 then
increasing to maximum value
if(n !=0)
{
for(int i = n; i > 0; i--)
{
fac *= i;
}
}
return fac;
}
//closing the function
int main()
{
int attempts = 0;
while(attempts < 3)
{
cout << "Enter a value you want to find the factorial of: ";
//print a prompt
int n;
// declaring the variable
cin >> n;
// user input the value n
if(n >= 0)
{
cout << "You entered " << n << "Therefore the factorial of "
<< n << "is : " << factorial (n);
break;
//lets us leave the while loop
}
else
{
cout << "Sorry you entered an invalid number; please try
enter a valid positive integer";
attempts++;
// increasing the number of attempts
if(attempts ==3)
cout << "\nFatal error: you have exceeded the number of
retries; sorry program is terminat\n";
}
}
return 0;
}
THE UNIVERSITY OF ZAMBIA
SCHOOL OF ENGINEERING
DEPARTMENT OF ELECTRICAL AND ELECTRONIC
ENGINEERING
Group members;
Oscar Jackson kachali
2021529215
Chama M Kachenga
2020012014
Owen Kabulula
2020021722
Lameck Nkowani
2020031990
Mulimo Nkonkomaliba
2020016630
Kachingwe Joshua
2020038129
Kevin chulu
2020066033
Download