Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- using namespace std;
- // This program displays all the square numbers up to a user input
- // Create an array with square numbers up to the limit entered sequentially
- vector<int> squares(int arraySize) {
- vector<int> squareList;
- for (int i = 1; i < arraySize; i++) {
- squareList.push_back(i * i);
- }
- return squareList;
- }
- int main() {
- cout << "Lists square numbers up to chosen limit" << endl << endl; // Two endl to leave a space before listing the numbers
- cout << "Please type in a number to use as a limit, then press 'Enter'" << endl;
- int limit;
- cin >> limit;
- cout << endl; // Visually split input number from output list
- int arraySize = int(sqrt(limit)) + 1; // We will need to know the size to make the array in a moment
- vector<int> squareList = squares(arraySize);
- for (int i : squareList) {
- cout << squareList[i] << endl;
- }
- cout << endl << "Press 'Enter' to exit program" << endl;
- cin.ignore(100, '\n'); // ignore any characters in the input buffer until we find an enter character
- cin.get(); // get one more char from the user
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement