Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main ()
- {
- int startNumber; //Initialize as an integer.
- int counter = 0; //Initialize with a value of 0.
- int result = 0; //Initialize with a value of 0.
- const int endNumber = 5000; //Initialize with a constant value of 5000.
- //Ask the user for a number to use for calculations and store that in
- //the startNumber variable.
- cout << "Please enter a starting number: ";
- cin >> startNumber;
- //Add the startNumber to itself and store in result until it is equal to
- //or more than the value in endNumber. Keep a running counter so that
- //we can report on how many loops it took to get at or over 5000.
- while (result < endNumber)
- {
- result = result + startNumber; //Add startNumber to the current value of result.
- counter++; //Increment the counter
- }
- //Return the end result
- cout << "It took " << startNumber << " added to itself " << counter << " times before it was greater than or equal to " << endNumber << "\n";
- cout << "The end value was: " << result;
- //Send an exit code (or error level) of 0.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment