AIR_FORCE_VET_GA2OK
AIR_FORCE_VET_GA2OK

Reputation: 1

I am trying to get a value to multiply at every iteration (in a loop) - (ex: 11 x 11) for 7 iterations

I am new to C++, and I am trying to construct a program in which a certain value (int) with multiply against itself for 7 iterations (and display its corresponding total).

EXAMPLE:

1st iteration - 0
2nd iteration - 11
3rd iteration - 121
4th iteration - 1331
5th iteration - 14641
6th iteration - 161051
7th iteration - 1771561

The code that I have used:

// This program will display a LOOP operation.
    
#include <iostream>
#include <iomanip>
    
using namespace std;
    
int main()
{
    double Tribbles = 0.0;
    
    int total = 0;
    int TPH = 11;
    
    // FOR LOOP
    
    for (int i = 0; i < 10; i++) {
    
        cout << "Total number of Tribbles in a 12-hour span: " << TPH << endl;
    
        TPH = (TPH * (i + 1));
    
    }
    // DO-WHILE LOOP
    string answer;
    
    do
    {
        cout << "Repeat: Y or N?";
        cin >> answer;
    
    
    } while (answer == "Y");
    
    // WHILE LOOP
    while (total < 10)
    
    {
    
        cout << "Total is: " << total << endl;
        total++;
    }
    
    return 0;
    
}

This code results in:

Total number of Tribbles in a 12-hour span: 11
Total number of Tribbles in a 12-hour span: 11
Total number of Tribbles in a 12-hour span: 22
Total number of Tribbles in a 12-hour span: 66
Total number of Tribbles in a 12-hour span: 264
Total number of Tribbles in a 12-hour span: 1320
Total number of Tribbles in a 12-hour span: 7920
Total number of Tribbles in a 12-hour span: 55440
Total number of Tribbles in a 12-hour span: 443520
Total number of Tribbles in a 12-hour span: 3991680
Repeat: Y or N?
Repeat: Y or N?n
Total is: 0
Total is: 1
Total is: 2
Total is: 3
Total is: 4
Total is: 5
Total is: 6
Total is: 7
Total is: 8
Total is: 9

I want to really get a solid understanding of C++ (so that I can move forward to Java, etc.)

Thanks for any assistance

Upvotes: 0

Views: 312

Answers (1)

Barmar
Barmar

Reputation: 781731

Your example isn't multiplying by itself each time, it's just multiplying by 11. 11 x 11 = 121, 121 x 11 = 1331, 1331 x 11 = 14641, etc.

So start with total = 1, and multiply that by TPH each iteration. Don't change TPH.

// This program will display a LOOP operation.
    
#include <iostream>
#include <iomanip>
    
using namespace std;
    
int main()
{
    
    int total = 1;
    int TPH = 11;
    
    // FOR LOOP
    
    for (int i = 1; i <= 7; i++) {
    
        cout << "Iteration " << i << ": " << total << endl;
    
        total *= TPH;
    
    }
    
    return 0;
}

Upvotes: 1

Related Questions