SlideShare uma empresa Scribd logo
1 de 14
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/

Mais conteúdo relacionado

Mais procurados

INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVERINSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVERDarwin Durand
 
Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
BLOOD DONATION SYSTEM IN C++
BLOOD DONATION SYSTEM IN C++BLOOD DONATION SYSTEM IN C++
BLOOD DONATION SYSTEM IN C++vikram mahendra
 
SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)SISTEMA DE FACTURACION (Ejemplo desarrollado)
SISTEMA DE FACTURACION (Ejemplo desarrollado)Darwin Durand
 
PVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error ExamplesPVS-Studio in 2021 - Error Examples
PVS-Studio in 2021 - Error ExamplesAndrey Karpov
 
How Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerHow Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerAndrey Karpov
 
BANK MANAGEMENT INVESTIGATORY PROJECT CLASS 12TH
BANK MANAGEMENT INVESTIGATORY PROJECT CLASS 12THBANK MANAGEMENT INVESTIGATORY PROJECT CLASS 12TH
BANK MANAGEMENT INVESTIGATORY PROJECT CLASS 12THSHAJUS5
 
Atm machine using c++
Atm machine using c++Atm machine using c++
Atm machine using c++Aqib Memon
 
Atm machine using c++
Atm machine using c++Atm machine using c++
Atm machine using c++Aqib Memon
 
Atm machine using c++
Atm machine using c++Atm machine using c++
Atm machine using c++Aqib Memon
 
Atm machine using c++
Atm machine using c++Atm machine using c++
Atm machine using c++Aqib Memon
 
Computer Science class 12
Computer Science  class 12Computer Science  class 12
Computer Science class 12Abhishek Sinha
 
Window object methods (timer related)
Window object methods (timer related)Window object methods (timer related)
Window object methods (timer related)Shivani Thakur Daxini
 
12. stl örnekler
12.  stl örnekler12.  stl örnekler
12. stl örneklerkarmuhtam
 
Telephone billing system in c++
Telephone billing system in c++Telephone billing system in c++
Telephone billing system in c++vikram mahendra
 
Computer mai ns project
Computer mai ns projectComputer mai ns project
Computer mai ns projectAayush Mittal
 
Sistema de ventas
Sistema de ventasSistema de ventas
Sistema de ventasDAYANA RETO
 

Mais procurados (19)

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

Semelhante a Loop Examples

Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solvingSyed Umair
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.pptLokeshK66
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++Dendi Riadi
 
Program membalik kata
Program membalik kataProgram membalik kata
Program membalik katahaqiemisme
 
Programación
ProgramaciónProgramación
ProgramaciónLuci2013
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
Programa Sumar y Multiplicar
Programa Sumar y MultiplicarPrograma Sumar y Multiplicar
Programa Sumar y MultiplicarAnais Rodriguez
 
Contoh program c++ kalkulator
Contoh program c++ kalkulatorContoh program c++ kalkulator
Contoh program c++ kalkulatorJsHomeIndustry
 
Computer Science Project on Management System
Computer Science Project on Management System Computer Science Project on Management System
Computer Science Project on Management System SajidAli643
 
JavaScript Control Statements II
JavaScript Control Statements IIJavaScript Control Statements II
JavaScript Control Statements IIReem Alattas
 
Output in c++ (cout)
Output in c++ (cout)Output in c++ (cout)
Output in c++ (cout)Ghada Shebl
 

Semelhante a Loop Examples (20)

Project in programming
Project in programmingProject in programming
Project in programming
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 
Lecture04
Lecture04Lecture04
Lecture04
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
Penjualan swalayan
Penjualan swalayanPenjualan swalayan
Penjualan swalayan
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Program membalik kata
Program membalik kataProgram membalik kata
Program membalik kata
 
Ch4
Ch4Ch4
Ch4
 
Lecture16
Lecture16Lecture16
Lecture16
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Programación
ProgramaciónProgramación
Programación
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Programa Sumar y Multiplicar
Programa Sumar y MultiplicarPrograma Sumar y Multiplicar
Programa Sumar y Multiplicar
 
Contoh program c++ kalkulator
Contoh program c++ kalkulatorContoh program c++ kalkulator
Contoh program c++ kalkulator
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Computer Science Project on Management System
Computer Science Project on Management System Computer Science Project on Management System
Computer Science Project on Management System
 
goto dengan C++
goto dengan C++goto dengan C++
goto dengan C++
 
JavaScript Control Statements II
JavaScript Control Statements IIJavaScript Control Statements II
JavaScript Control Statements II
 
Output in c++ (cout)
Output in c++ (cout)Output in c++ (cout)
Output in c++ (cout)
 

Último

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Último (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Loop Examples

  • 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/