diff options
| author | austinlujan <[email protected]> | 2024-03-21 03:53:27 -0700 |
|---|---|---|
| committer | austinlujan <[email protected]> | 2024-03-21 03:53:27 -0700 |
| commit | d1dba197cae53aaff3616df61fd1a0d8d5f26f97 (patch) | |
| tree | 84774576f2a66ac33426673dee4a23d699b2a094 /Project1/program.cpp | |
| parent | init commit (diff) | |
| download | homework-3-austinlujan-main.tar.xz homework-3-austinlujan-main.zip | |
Diffstat (limited to 'Project1/program.cpp')
| -rw-r--r-- | Project1/program.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Project1/program.cpp b/Project1/program.cpp new file mode 100644 index 0000000..2fb4727 --- /dev/null +++ b/Project1/program.cpp @@ -0,0 +1,48 @@ +#include <iostream> +#include "helpers.h" + +long factorial(int n) { + if (n == 0 || n == 1) + return 1; + else + return n * factorial(n - 1); +} + +int fibonacci(int n) { + if (n <= 1) + return n; + else + return fibonacci(n - 1) + fibonacci(n - 2); +} + +double power(double base, int exponent) { + if (exponent == 0) + return 1; + else if (exponent > 0) + return base * power(base, exponent - 1); + else + return (1 / base) * power(base, exponent + 1); +} + +int main() { + int n; + double base; + int exponent; + + // Factorial calculation + std::cout << "Enter a number to calculate its factorial: "; + std::cin >> n; + std::cout << "Factorial of " << n << " is: " << factorial(n) << std::endl; + + // Fibonacci sequence + std::cout << "Enter the term number to find in Fibonacci sequence: "; + std::cin >> n; + std::cout << "Term number " << n << " in Fibonacci sequence is: " << fibonacci(n) << std::endl; + + // Power function + std::cout << "Enter base and exponent to calculate power (base (space) exponent): "; + std::cin >> base >> exponent; + std::cout << base << " raised to the power of " << exponent << " is: " << power(base, exponent) << std::endl; + + return 0; +}
\ No newline at end of file |