evetsleep

While Example

Oct 25th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1.     #include <iostream>
  2.  
  3.     using namespace std;
  4.  
  5.     int main ()
  6.     {
  7.         int startNumber; //Initialize as an integer.
  8.         int counter = 0; //Initialize with a value of 0.
  9.         int result = 0; //Initialize with a value of 0.
  10.         const int endNumber = 5000; //Initialize with a constant value of 5000.
  11.  
  12.         //Ask the user for a number to use for calculations and store that in
  13.         //the startNumber variable.
  14.         cout << "Please enter a starting number: ";
  15.         cin >> startNumber;
  16.        
  17.         //Add the startNumber to itself and store in result until it is equal to
  18.         //or more than the value in endNumber.  Keep a running counter so that
  19.         //we can report on how many loops it took to get at or over 5000.
  20.         while (result < endNumber)
  21.         {
  22.             result = result + startNumber; //Add startNumber to the current value of result.
  23.             counter++; //Increment the counter
  24.         }
  25.  
  26.         //Return the end result
  27.         cout << "It took " << startNumber << " added to itself " << counter << " times before it was greater than or equal to " << endNumber << "\n";
  28.         cout << "The end value was: " << result;
  29.    
  30.         //Send an exit code (or error level) of 0.
  31.         return 0;
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment