Upcoming SlideShare
×

# Powerpoint loop examples a

684 views

Published on

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Powerpoint loop examples a

1. 1. Loop example 1 http://eglobiotraining.com.#include <iostream>using namespace std;int main(){ int counter = 0; // Initialize counter to 0. int numTimes = 0; // Variable for user to enter the amount of times. cout << "How many times do you want to see Einstein?: "; cin >> numTimes; // This is how many times the loop repeats. cout << "n"; while (counter < numTimes) // Counter is less than the users input. { cout << "Einstein!n"; counter++; // Increment the counter for each loop. } cout << "n"; return 0;}
2. 2. Screenshot http://eglobiotraining.com./ *===============================[outpu t]==================================== How many times do you want to see Einstein?: 6 Einstein! Einstein! Einstein! Einstein! Einstein! Einstein! Press any key to continue . . . ===================================== ===================================== ===*/
3. 3. Loop Example 2 http://eglobiotraining.com.#include "math.h"#include <iostream>#include <cmath>#include <iomanip>using namespace std;using std::cout;using std::endl;using std::cin;//mortgage class//class Mortgage{ public: void header(); void enterprinc(); double princ; double anInt; int yrs;};//define function//void Mortgage::header(){ cout << "Welcome to Smiths Amortization Calculatornn"; cout << endl; cout << "Week 3 Individual Assignment C++ Mortgage Amortization Calculatornn"; cout << endl;}void Mortgage::enterprinc(){ cout << endl; cout << "Enter the amount The Mortgage Amount:\$"; cin >> princ;}
4. 4. http://eglobiotraining.com.//main function//int main (){Mortgage mortgageTotal; double princ = 0; double anInt [4]= {0, 5.35, 5.5, 5.75,}; double yrs [4]= {0, 7, 15, 30,}; double pmt = 0; double intPd = 0; double bal = 0; double amtPd = 0; double check(int min, int max); int NOP = 0; int Mnth = 0; int SL = 0; char indicator = (A ,a,R,r,D,d); int select;
5. 5. http://eglobiotraining.com.//Begin Loop{ mortgageTotal.header (); mortgageTotal.enterprinc (); cout<< "Please Enter Your Selection!"<< endl; cout<< "Press A to make selection for term of mortgage and interest rate"<<endl; cin >> indicator; if (indicator == A,a) { cout << "Please select your Terms and Rates from the following choices: " << endl; cout << "1. 7 years at 5.35%" << endl; cout << "2. 15 years at 5.5%" << endl; cout << "3. 30 years at 5.75%" << endl; cout << endl; cout << "What is your selection:"; select = 0; cin >> select; cout << endl << endl << endl; switch (select) {
6. 6. http://eglobiotraining.com.case 0: cout << endl; break; case 1: yrs[2] = 7; anInt[2] = 5.35; cout << "You have selected a 7 year mortgage at 5.35% interest." << endl; break; case 2: yrs[3] = 15; anInt[3]= 5.50; cout << "You have selected 15 year mortgage at 5.50 interest%" << endl; break; case 3: yrs[4] = 30; anInt[4] = 5.75; cout << "You have selected a 30 year mortgage at 5.75% interest" << endl; break; default: cout << "Invalid Line Number" << endl; if ((select < 1)|| (select > 3)) { system("cls"); cout << "Your choice is invalid, please enter a valid choice!!!"; system("PAUSE"); system("cls"); return main(); } } }
7. 7. http://eglobiotraining.com.//Formulas//double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); // notice the variable in the brackets that i added. cout << "Your Monthly Payment is: \$" << pmt << "n"; cout << endl;double NOP = yrs [select] * 12; SL = 0; for (Mnth = 1; Mnth <= NOP; ++Mnth) { intPd = mortgageTotal.princ * (anInt[select] / 1200); amtPd = pmt - intPd; bal = mortgageTotal.princ - amtPd; if (bal < 0)bal = 0; mortgageTotal.princ = bal; if (SL == 0) { cout << "Balance of Loan" << "tttAmount of Interest Paid" << endl; } cout << setprecision(2) << fixed << "\$" << setw(5) << bal << "tttt\$"<< setw(5) << intPd << endl; ++SL;
8. 8. http://eglobiotraining.com.//Allows user to decide whether or not to continue// if (SL == 12) { cout << "Would you like to continue the amoritization or quit the program?n"; cout << endl; cout << "Enter R to see the remainder or D for done.n"; cin >> indicator; if (indicator == R,r)SL = 0; else if (indicator == D,d) cout << endl; }}}return 0;}
9. 9. Loop example 3 http://eglobiotraining.com.#include <iostream>using namespace std;int main(){ int number = 0; // Variable for user to enter a number. int sum = 0; // To hold the running sum during all loop iterations. cout << "Enter a number: "; cin >> number; // Loop keeps adding until it reaches the number entered. for (int index = 0; index <= number; index++) { sum += index; // Each iteration Adds to the variable sum. } // cout statement is put after the loop. cout << "nThe sum of all numbers from 0 to " << number << " equals: " << sum << "nn"; return 0;}
10. 10. Screenshot http://eglobiotraining.com.*===========================[output]==== =============================== Enter a number: 6 The sum of all numbers from 0 to 6 equals: 21 Press any key to continue . . . ===================================== ===================================*/
11. 11. Loop example 4 http://eglobiotraining.com.#include <iostream>using namespace std;int main(){ char selection; cout<<"n Menu"; cout<<"n========"; cout<<"n A - Append"; cout<<"n M - Modify"; cout<<"n D - Delete"; cout<<"n X - Exit"; cout<<"n Enter selection: "; cin>>selection; switch(selection){ case A : {cout<<"n To append a recordn";} break; case M : {cout<<"n To modify a record";} break; case D : {cout<<"n To delete a record";} break; case X : {cout<<"n To exit the menu";} break; // other than A, M, D and X... default : cout<<"n Invalid selection"; // no break in the default case } cout<<"n"; return 0;}
12. 12. Screenshothttp://eglobiotraining.com.
13. 13. Loop Example 5 http://eglobiotraining.com.#include <iostream>using namespace std;int main (){ for( ; ; ) { printf("This loop will run forever.n"); } return 0;}
14. 14. Submitted to:Professor Erwin Globiohttp://eglobiotraining.com/