dimanche 26 septembre 2021

Trying to write a C++ program that lists all the polite numbers in the upper value

For example, if the user enters the number 21, I want all the polite numbers listed. so for 21, the output would be 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21. I am new for loops and I am having a difficult time trying to get this to work. I cant include any global variables for this assignment. Any help would be greatly appreciated. Thank you.

Here is my code so far:

 #include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main() {

    //Display a welcome message
    std::cout << "Welcome to the polite numbers program" << std::endl;

    int upValue;
    int p3=3;
    int p5=5;

    cout << "What is the upper value? "; //Ask user for upper limit
    cin >> upValue; //Stores user input into upValue

    while (upValue < 1) //Validate user input of upper limit
    {
        cout << "What is the upper value? (must be an integer > 0) ";
        cin >> upValue;
    }

    
    if (upValue==3) //only display polite num 3
    {
        cout<<p3<<endl;
    }
    else if (upValue==5) //only display polite number 5 and 3
    {
        cout<<p3<<endl;
        cout<<p5<<endl;
    }
    else {
        cout << p3 << endl; 
        cout << p5 << endl;
        for (int curSeq = 2; curSeq <= upValue; curSeq++) {
            int n;
            int nextPNum = n + (log((n + (log(n) / log(2))))) / log(2);
            cout << nextPNum << endl;

        }
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire