C++/Language/For

Материал из C\C++ эксперт
Перейти к: навигация, поиск

for loop: A Multiplication Table

#include<iostream>
using namespace std;
int main(void)
{
     cout << "A multiplication table:" << endl
          << "  1\t2\t3\t4\t5\t6\t7\t8\t9" << endl
          << "" << endl;
     for(int c = 1; c < 10; c++)
     {
          cout << c << "| ";
          for(int i = 1; i < 10; i++)
          {
          cout << i * c << "\t";
          }
          cout << endl;
     }
     return 0;
}


For loop with int value type as the control

#include <iostream>
using namespace std;
int main()
{
  int b, e, r;
  cout << "Enter base: ";
  cin >> b;
  cout << "Enter exponent: ";
  cin >> e;
  r = 1;
  for( ; e; e--) 
     r = r * b;
  
  cout << "Result: " << r;
  return 0;
}


For statement

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
   double rate = 1.15;   
                         
   cout << fixed << setprecision(2);
   cout << "Euro             Dollar\n";
   for( int euro = 1; euro <= 5; ++euro)
     cout << "          " << euro
          << "          " << euro*rate << endl;
   return 0;
}