day 2 commit & changed day 1 filenames

This commit is contained in:
bMorgan01 2020-09-09 20:11:34 -06:00
parent d5f4f817c0
commit 703fcae148
6 changed files with 99 additions and 0 deletions

19
D2_factorial.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <iostream>
int factorial(int n) {
if (n <= 0) {
return -1;
} else if (n > 1) {
return n * factorial(n - 1); //n * n-1 * n-2....
} else return 1;
}
int main() {
int n;
std::cout << "Enter n to do n!: ";
std::cin >> n;
std::cout << "Ans: " << factorial(n) << std::endl;
return 0;
}

50
D2_looping.cpp Normal file
View file

@ -0,0 +1,50 @@
#include <iostream>
#include <vector>
int main() {
std::vector<int> list; //= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
int arr[10]; //= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
//initialize vector
for (int i = 0; i < 10; i++) {
list.push_back(i);
}
//initialize array
for (int i = 0; i < 10; i++) {
arr[i] = i;
}
int n;
std::cout << "Enter number: ";
std::cin >> n;
//standard for loop
for (int i = 0; i < n; i++) {
std::cout << list.at(i%10) << " ";
}
std::cout << std::endl;
//while loop
int j = 0;
while (j < n) {
std::cout << j << " ";
j++;
}
std::cout << std::endl;
//range based loop w/ vector
for (int k : list) {
std::cout << k << " ";
}
std::cout << std::endl;
//range based loop w/ array
for (int l : arr) {
std::cout << l << " ";
}
std::cout << std::endl;
return 0;
}

BIN
D2_loops.pdf Normal file

Binary file not shown.

30
D2_power.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <iostream>
int power(int n, int e) {
if (n < 0 || e < 0) return -1;
else if (e > 0) return n * power(n, e - 1);
else return 1;
}
int main() {
int n, e;
std::cout << "Enter number: ";
std::cin >> n;
std::cout << "Enter exponent: ";
std::cin >> e;
std::cout << "Ans: " << power(n, e) << std::endl;
return 0;
/*
* Example:
*
* 5^4
*
* 5 * 5^3 =
* 5 * 5 * 5^2 =
* 5 * 5 * 5 * 5^1 =
* 5 * 5 * 5 * 5 * 5^0 = 625
*/
}

View file