SlideShare a Scribd company logo
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;
}
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 . . .
    =====================================
    =====================================
    ===*/
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 Smith's 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;

}
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;
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)
                          {
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();
                                             }
                        }
                        }
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;
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;
}
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;
}
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 . . .
  =====================================
  ===================================*/
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;
}
Screenshot
http://eglobiotraining.com.
Loop Example 5
                         http://eglobiotraining.com.

#include <iostream>
using namespace std;

int main ()
{

    for( ; ; )
    {
      printf("This loop will run forever.n");
    }

    return 0;
}
Submitted to:
Professor Erwin Globio
http://eglobiotraining.com/

More Related Content

PPTX
Fekra c++ Course #2
PPTX
Programming - Marla Fuentes
TXT
DOCX
VISUALIZAR REGISTROS EN UN JTABLE
PDF
Rajeev oops 2nd march
PDF
Mutation testing in php with Humbug
DOC
Ip project
PPTX
Efficient JavaScript Mutation Testing
Fekra c++ Course #2
Programming - Marla Fuentes
VISUALIZAR REGISTROS EN UN JTABLE
Rajeev oops 2nd march
Mutation testing in php with Humbug
Ip project
Efficient JavaScript Mutation Testing

What's hot (19)

DOCX
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
PPTX
C programming
PPT
Mocking Dependencies in PHPUnit
DOC
BLOOD DONATION SYSTEM IN C++
PDF
SISTEMA DE FACTURACION (Ejemplo desarrollado)
PDF
PVS-Studio in 2021 - Error Examples
PPTX
How Data Flow analysis works in a static code analyzer
PDF
BANK MANAGEMENT INVESTIGATORY PROJECT CLASS 12TH
DOC
Atm machine using c++
DOC
Atm machine using c++
DOC
Atm machine using c++
DOC
Atm machine using c++
PDF
Computer Science class 12
PPTX
Window object methods (timer related)
DOC
12. stl örnekler
TXT
CSNB244 Lab5
DOC
Telephone billing system in c++
DOC
Computer mai ns project
PDF
Sistema de ventas
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
C programming
Mocking Dependencies in PHPUnit
BLOOD DONATION SYSTEM IN C++
SISTEMA DE FACTURACION (Ejemplo desarrollado)
PVS-Studio in 2021 - Error Examples
How Data Flow analysis works in a static code analyzer
BANK MANAGEMENT INVESTIGATORY PROJECT CLASS 12TH
Atm machine using c++
Atm machine using c++
Atm machine using c++
Atm machine using c++
Computer Science class 12
Window object methods (timer related)
12. stl örnekler
CSNB244 Lab5
Telephone billing system in c++
Computer mai ns project
Sistema de ventas
Ad

Similar to Powerpoint loop examples a (20)

PPTX
Project in programming
DOCX
Assignement of programming & problem solving
PPT
Lecture04
PPT
ch5_additional.ppt
PPTX
Lec 2.pptx programing errors \basic of programing
PPTX
Computational PhysicsssComputational Physics.pptx
TXT
Penjualan swalayan
PPT
FP 201 - Unit 3 Part 2
DOCX
Tugas praktikukm pemrograman c++
DOCX
Program membalik kata
PPT
PPT
Lecture16
PDF
C++ TUTORIAL 3
PPTX
Computational Physics Cpp Portiiion.pptx
PPTX
Object Oriented Programming Using C++: C++ Exception Handling.pptx
DOCX
Programación
PPT
FP 201 Unit 3
DOCX
Programa Sumar y Multiplicar
PDF
Contoh program c++ kalkulator
PDF
4th_Ed_Ch03.pdf
Project in programming
Assignement of programming & problem solving
Lecture04
ch5_additional.ppt
Lec 2.pptx programing errors \basic of programing
Computational PhysicsssComputational Physics.pptx
Penjualan swalayan
FP 201 - Unit 3 Part 2
Tugas praktikukm pemrograman c++
Program membalik kata
Lecture16
C++ TUTORIAL 3
Computational Physics Cpp Portiiion.pptx
Object Oriented Programming Using C++: C++ Exception Handling.pptx
Programación
FP 201 Unit 3
Programa Sumar y Multiplicar
Contoh program c++ kalkulator
4th_Ed_Ch03.pdf
Ad

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Final Presentation General Medicine 03-08-2024.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program

Powerpoint loop examples a

  • 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. 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. 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 Smith's 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. 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. 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. 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. 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. 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. 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. 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. 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; }
  • 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. Submitted to: Professor Erwin Globio http://eglobiotraining.com/