Friday, December 5, 2014

C++ using Loop and Input and Output Progam



Break and continue
 
#include 
#include
using namespace std;
 int main() 
{
   int number = 1;
   while (true) 
     {
      ++number;
      if ((number % 3) == 0) continue;
      if (number == 133) break;
      if ((number % 2) == 0) 
     {
         number += 3;
      } 
      else 
      {
         number -= 3;
      }
      cout << number << " ";
   }
   cout << endl;
   return 0;
}
 
Nested Loops
 
#include 
#include
using namespace std;
int main() 
{
   int size = 8;
   for (int row = 1; row <= size; ++row) 
   {   
      for (int col = 1; col <= size; ++col) 
   {  
         cout << "# ";
      }
      cout << endl; 
   }
    return 0;
}

String Input & output


#include 
#include
#include      
#include 
using namespace std;  
int main() 
{
   string message("Hello");
   cout << message << endl;
   cout << "Enter a message (no space): ";
   cin >> message;
   cout << message << endl;
   cin.ignore(numeric_limits::max(), '\n');
   cout << "Enter a message (with spaces): ";
   getline(cin, message);  
   cout << message << endl;
   return 0;
}
 

 Output Formatting

 
#include 
#include
#include    
using namespace std;
int main() 
{
   double pi = 3.14159265;
   cout << fixed << setprecision(4); 
   cout << pi << endl;
   cout << "|" << setw(8) << pi << "|" << setw(10) << pi << "|" << endl;
   cout << setfill('-');
   cout << "|" << setw(8) << pi << "|" << setw(10) << pi << "|" << endl;
   cout << scientific;  
   cout << pi << endl;
   bool done = false;
   cout << done << endl;  
   cout << boolalpha;     
   cout << done << endl;
   return 0;
}

Input Formatting

 
#include 
#include
#include 
#include 
using namespace std;
 int main() 
{
   string areacode, phonecode;
   string instr;
   cout << "Enter your phone number in this format (xxx)xxx-xxxx : ";
   cin.ignore();   
   cin >> setw(3) >> areacode;
   cin.ignore();   
   cin >> setw(3) >> phonecode;
   cin.ignore();   
   cin >> setw(4) >> instr;
   phonecode += instr;
   cout << "Phone number is (" << areacode << ")"<< phonecode.substr(0, 3) << "-"<< phonecode.substr(3, 4) << endl;  
   return 0;
}
 

  Array and Loop

 
#include 
#include
#include 
#include 
#define size 7
using namespace std;
int main() 
{
   int marks[] = {74, 43, 58, 60, 90, 64, 70};
   int sum = 0;
   int sumsq = 0;
   double mean, stddev;
   for (int i = 0; i < size; ++i) 
  {
      sum += marks[i];
      sumsq += marks[i]*marks[i];
   }
   mean = (double)sum/size;
   cout << fixed << "Mean is " << setprecision(2) << mean << endl;
   stdDev = sqrt((double)sumsq/size - mean*mean);
   cout << fixed << "Std dev is " << setprecision(2) << stddev << endl;
   return 0;
}

Range-based for loop

 
#include 
#include
using namespace std;
int main() 
{
   int numbers[] = {11, 22, 33, 44, 55};
   for (int number : numbers) 
  {
      cout << number << endl;
   }
   for (int &number : numbers)  
  {
      number = 99;
   }
   for (int number : numbers) 
  {
      cout << number << endl;
   }
   return 0;
}

No comments:

Post a Comment