C++ Tutorial #6 — Nested if statements

Topics Discussed: If statements, using bool(ean) operators as custom if-conditionals.

``````
#include <iostream>

using namespace std;

int main() {
double accountBalance = 5000.0, withdrawAmount = 0;

cout << "How much money would you like to withdraw? ";
cin >> withdrawAmount;

if (withdrawAmount > accountBalance) {
cout << "Error: The amount you tried to take out is greater than your current balance.";
} else if (withdrawAmount > 1000) {
cout << "It looks like you're trying to take out a lot of money, Press 1 to confirm, "
"press 2 to decline this transaction to withdraw of:  " << withdrawAmount << " --";
int confirm = 0;
cin >> confirm;
if (confirm == 1) {
cout << "Confirmation accepted You withdrew: " << withdrawAmount <<
" -- Your remaining account balance is:  " << accountBalance - withdrawAmount;
} else {
cout << "Transaction declined by user.";
}
} else {
cout << "You withdrew: " << withdrawAmount << " -- Your remaining account balance is:  " << accountBalance - withdrawAmount;
}

return 0;
}
``````

Programming Challenge #1 — Primes

Code difficulty: 3/10
Math difficulty: 4/10

In this challenge I ask that you do the following things:

Create a program that will:

1. Allow a user to input a number
2. Allow the user to see if the number is prime or not
3. If the number is not prime, tell the user what number it is divisible by
4. Use a function to process whether or not the value is prime (this idea will be used in a future challenge
5. Use double or Long for increased number length

Concepts used: Functions, variables, loops, arithmatic functions, breaks, boolean

You should be able to do this by: Lesson 33

My solution: http://ideone.com/AKmCm (spoilers- Don’t look unless you are really stuck!)

Please note that my solutions are not checked thoroughly, and should only be used for reference. They are often non-optimal solutions that could be improved upon (by you!). I keep these intentionally rough for just that reason.

Programming Challenge #2 – Dual Primes

Code difficulty: 4/10
Math difficulty: 4/10

Before we begin here: Q. What is a dual prime?
A. A dual prime is 2 prime numbers that are exactly “2” apart. Example: 3, 5 // 11,13, etc.

In his challenge I ask that you do the following things:

Create a program that will:

1. Allow a user to input a number to iterate up to
2. Allow the user to see all the dual primes, and a list of the numbers that are NOT dual prime
3. Use either a vector or a list and their inherent member functions to handle the data from the loop

Concepts used: Functions, variables, loops, arithmatic functions, breaks, boolean, vectors (or lists), template class member functions

You should be able to do this by: Lesson 44

My solution: http://ideone.com/Y7wiJ (spoilers- Don’t look unless you are really stuck!)

Please note that my solutions are not checked thoroughly, and should only be used for reference. They are often non-optimal solutions that could be improved upon (by you!). I keep these intentionally rough for just that reason.