evetsleep

For Example

Oct 25th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 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; //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.         //While result is less than endNumber, keep adding startNumber to it.
  18.         //Keep track of the counter so that we can determine how many times
  19.         //we had to loop through before the condition was met.
  20.         for (counter = 0; result < endNumber; counter++)
  21.         {
  22.             result = result + startNumber;
  23.         }
  24.  
  25.         //Return the end result
  26.         cout << "It took " << startNumber << " added to itself " << counter << " times before it was greater than or equal to " << endNumber << "\n";
  27.         cout << "The end value was: " << result;
  28.    
  29.         //Send an exit code (or error level) of 0.
  30.         return 0;
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment