#include int smallest(const int w, const int x, const int y, const int z) { if (w < x && w < y && w < z) return w; if (x < w && x < y && x < z) return x; if (y < w && y < x && y < z) return y; return z; } int main() { cout << "Type a number: "; const int a = read_int(); cout << "Type a number: "; const int b = read_int(); cout << "Type a number: "; const int c = read_int(); cout << "Type a number: "; const int d = read_int(); cout << "The smallest is " << smallest(a, b, c, d) << "\n"; } _______________________________________________________________________ /* const int name = value; int name = value; // same thing, but name's value can change at any time. Variable name = newvalue; // changes that value of name. Assignment variant int name; // introduces a new variable, you don't know what its value is */ ______________________________________________________________________ #include int smallest(const int w, const int x, const int y, const int z) { int s = w; if (x < s) s = x; if (y < s) s = y; if (z < s) s = z; return s; } int main() { cout << "Type a number: "; const int a = read_int(); cout << "Type a number: "; const int b = read_int(); cout << "Type a number: "; const int c = read_int(); cout << "Type a number: "; const int d = read_int(); cout << "The smallest is " << smallest(a, b, c, d) << "\n"; } ______________________________________________________________________ bool go_again() { bool answer, valid = false; while (! valid) { cout << "Do you want another go? "; const string s = read_string(); if (s == "yes") { answer = true; valid = true; } if (s == "no") { answer = false; valid = true; } } return answer; } int main() { while (go_again()) { print("Type a number: "); const int number = read_int(); say_number(number); new_line(); } } _________________________________________________________________ bool go_again() { bool answer, valid = false; while (! valid) { cout << "Do you want another go? "; const string s = read_string(); if (s == "yes") { answer = true; valid = true; } if (s == "no") { answer = false; valid = true; } } return answer; } int main() { while (true) { print("Type a number: "); const int number = read_int(); say_number(number); new_line(); if (go_again() == false) break; } } __________________________________________________________________ #include using namespace std; bool is_leap_year(const int y) { if (y % 400 == 0) return true; if (y % 100 == 0) return false; if (y % 4 == 0) return true; return false; } int day_of_year(const int y, const int m, const int d) { int day = 0; if (m > 1) day = day + 31; if (m > 2) if (is_leap_year(y)) day = day + 29; else day = day + 28; if (m > 3) day = day + 31; if (m > 4) day = day + 30; if (m > 5) day = day + 31; if (m > 6) day = day + 30; if (m > 7) day = day + 31; if (m > 8) day = day + 31; if (m > 9) day = day + 30; if (m > 10) day = day + 31; if (m > 11) day = day + 30; day = day + d; return day; } int main() { cout << "Enter Y M D: "; int year, month, day; cin >> year >> month >> day; cout << year << "/" << month << "/" << day << " is day number " << day_of_year(year, month, day) << "\n"; } _____________________________________________ #include using namespace std; double factorial(const int N) { int i = 1; double f = 1; while (i <= N) { f = f * i; i = i + 1; } return f; } int main() { cout << "Type a number: "; int x; cin >> x; cout << x << "! = " << factorial(x) << "\n"; } ___________________________________________________________________ #include using namespace std; double factorial(int N) { double f = 1; while (N > 0) { f = f * N; N = N - 1; } return f; } int main() { cout << "Type a number: "; int x; cin >> x; cout << x << "! = " << factorial(x) << "\n"; }