jeudi 11 février 2021

How to increment each printed row to the power 2 to the power of n and n-1 in C++?

Trying to write a program in C++ that prints rows of "*" based on the result of 2^n then after a certain number is entered decrease 2^n-1. For example any where between 0 and 6 (both of these being n) a row of stars is printed from *, **, **** and so forth. Then anything between 7 and 12 the number of stars decreases 2^n-1.

So far I have this but it only prints one singe line of * based on the result of the exponent.

int base = 2, exponent, result;
    char star = '*';

    cout << "Enter a number between 0 and 12:  ";
    cin >> exponent;

    result = pow(base, exponent);
    string pattern = string(result, star);
    cout << pattern << endl;

If user enters 4 
Output; ********

Goal for it to look like this

2^0 = *
2^1 = **
2^2 = ****
2^3 = ********
2^4 = ****************
2^5 = ********
2^6 = ****
2^7 = **
2^8 = *

Hope that makes sense.

Aucun commentaire:

Enregistrer un commentaire