Wednesday, June 20, 2018

Binomial Coefficient Using While Loops

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double i, N, factor;

    while (i > 0)
    {
        cout << "Option 1: (1+i)^N" << endl;
        cout << "Option 2: 1 / (1+i)^N" << endl;
        cout << "Option 3: ((1+i)^N-1)/i" << endl;
        cout << "Option 4: ((1+i)^N-1) / (i(1+i))^N" << endl;
        cout << "Option 5: i / ((1+i)^N-1" << endl;
        cout << "Option 6: (i(1+i)^N) / ((1+i)^N-1" << endl;
        cout << "Choose from 1 to 6 (or -1 to end program):\n";
            cin >> i;

        if (i == 1)
        {
            cout << "Enter i: " << endl;
                cin >> i;
            cout << "Enter N: " << endl;
                cin >> N;

            factor = pow((1+i), N);

            cout << "Answer for option 1 is: " << factor << endl;
        }

        else if (i == 2)
        {
            cout << "Enter i: " << endl;
                cin >> i;
            cout << "Enter N: " << endl;
                cin >> N;

            factor = 1 / pow((1+i), N);

            cout << "Answer for option 2 is: " << factor << endl;
        }

        else if (i == 3)
        {
            cout << "Enter i: " << endl;
                cin >> i;
            cout << "Enter N: " << endl;
                cin >> N;

            factor = pow((1+i), N) / i;

            cout << "Answer for option 3 is: " << factor << endl;
        }

        else if (i == 4)
        {
            cout << "Enter i: " << endl;
                cin >> i;
            cout << "Enter N: " << endl;
                cin >> N;

            factor = pow((1+i), N);

            cout << "Answer for option 4 is: " << (factor - 1) / (i*factor) << endl;
        }

        else if (i == 5)
        {
            cout << "Enter i: " << endl;
                cin >> i;
            cout << "Enter N: " << endl;
                cin >> N;

            factor = pow((1+i), N);

            cout << "Answer for option 5 is: " << i/(factor - 1) << endl;
        }

        else if (i == 6)
        {
            cout << "Enter i: " << endl;
                cin >> i;
            cout << "Enter N: " << endl;
                cin >> N;

            factor = pow((1+i), N);

            cout << "Answer for option 6 is: " << (i*factor) / (factor - 1) << endl;
        }

        else if (i > 6)
        {
            cout << "Incorrect option entered. Enter a new option number!" << endl;
        }

            if (i == -1)
            {
                return 0;
            }

    }

}

Friday, June 1, 2018

Total Grade Using Your Own Inputs

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector <double> data;

    double input, input2;
    double total = 0, total2 = 0;

    cout << "\aEnter your score for each quiz, tests and/or homework (enter 0 to end): " << endl;

    do{
        cin >> input;
        data.push_back (input);

        total += input;

    } while (input != 0); // once input is 0, loop ends

    cout << "\aEnter max score for each quiz, tests and/or homework (enter 0 to end): " << endl;

    do{
        cin >> input2;
        data.push_back (input2);

        total2 += input2;

    } while (input2 != 0);

    data.pop_back();    // prevents counting 0, removes 1 array
                        // since both do loops uses data, the pop_back applies to both. If you want to use a different pop_back
                        // then you will need to change the variable, in this case: data

 //   int dataSize = int(data.size());
/* if and else to prevent dividing by 0 */
    if (total2 == 0)
        cout << "Undefined!" << endl;
    else
    cout << "Your grade is: " << (total/total2)*100 << "%" << endl;

}

Tuesday, January 16, 2018

Write a program that reads in 7 natural numbers and then writes out how many of them are divisible by 4

#include <iostream>
using namespace std;

int main()
{
    cout << "Enter a number that might be divisible by 4: " << endl;

    for (int i = 0; i < 7; i++)
    {
        int x;
        cin >> x;

       if (x % 4 == 0)
        cout << "It's divisible by 4!" << endl;

       else
       cout << "It's NOT divisible by 4" << endl;
    }

    return 0;
}

Write a program that reads in one number specifying the length of the film in seconds and then writes out the length of the film in hours, remaining minutes and remaining seconds.

// Chapter 15 - Exercise 3
#include <iostream>
using namespace std;

int main()
{
    cout << "Enter length of movie in minutes: " << endl;

    int length;
    cin >> length;

    int a = length / 60, b = length % 60, c = b * 60;

    cout << a << "hours & " << b << "min & " << c << "sec" << endl;

    return 0;
}