Pages

Sunday, September 01, 2013

Write a program of Adding a list of number in C++

Add a list of integer from keyboard

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
   #include <iostream.h>
   #include <conio.h>
   using namespace std;
   int main ()
     {
       int x;
       int sum = 0;
       
      cout << " Enter your number: <EOF> to stop \n";
      while (cin >> x)
           sum += x;
      cout << "\n The total number: " << sum << endl;
      getch ();
      return 0;
             } // main       

Result:


 Enter your number: <EOP> to stop
15
22
3^d
 The total is: 40

Repetition

Write a program of A while loop to print number in C++

Simple while loop that prints numbers 10 per line

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   #include <iostream.h>
   #include <conio.h>
   #include <iomanip.h>
   int main ()
     {
      cout << " Enter an integer between 1 and 100:  ";
      int num;
      cin  >> num;
      
   // Test number
   if (num > 100)
       num = 100;
   int lineCount = 0;
   while (num > 0)
    {
      if (lineCount < 10)
          lineCount++;
      else
      {
          cout << endl;
          lineCount = 1;
          }// else
   cout << setw(4) << num--;
        } //while
        getch ();
        return 0;
               } // main  

Result:


 Enter an integer between 1 and 100:  15
  15  14  13  12  11  10   9   8   7   6
   5   4   3   2   1 

Write a program of Process-control system example in C++


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 while (1)
 {
  temp = getTemperture();
       if (temp < 68)
     turnOnHeater();
  else if (temp > 78)
     turnOnAirCond();
  else
  {
      turnOfHeater();
      turnOfAirCond();
      } // main
           } // while(1)

Write a program that convert score to grade in C++

This program reads a test score .Calculate the letter grade based on the absolute scale, and print it

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   #include <iostream.h>
   #include <conio.h>
   
   char scoreToGrade (int score);
   
   int main ()
      {
       cout << " Enter the test score(0-100): ";
       int score;
       cin  >> score ;
       
      char grade = scoreToGrade (score);
      cout << " The grade is: " << grade << endl;
      getch();
      return 0;
             } //main
   /* ============= scoreToGrade ===================
   This function calculates the letter grade for a score
   Pre  the parameter score
   Post Return the grade                     */
   char scoreToGrade (int score)
   {
    char grade;
   
   
        if (score >= 90)
             grade = 'A';
   else if (score >=80)
             grade = 'B';
   else if (score >=70)
             grade = 'C';
   else if (score >=60)
             grade = 'D';
   else 
             grade = 'F';
   getch ();
   return grade;    
                }// scoreToGrade 

Result:


 Enter the test score (0-100): 91
 The grade is: A

Write a Program of Student Grading in C++

This program reads a test score, calculates the letter grade for the score, print the grade

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   #include <iostream.h>
   #include <conio.h>
   
   char scoreToGrade (int score);
   
   int main ()
      {
      cout << " Enter the test score (0-100): ";
      int score;
      cin  >> score;
      
     char grade = scoreToGrade (score);
      cout << " The grade is: " << grade << endl;
      getch();
      return 0;
             } //main
   /* ============= scoreToGrade ===================
   This function calculates the letter grade for a score
   Pre  the parameter score
   Post Return the grade                     */
   char scoreToGrade (int score)
   {
     int temp = score / 10;
   char grade;
   switch (temp)
     {     
       case 10 :
       case  9 : grade = 'A';
                break; 
       case  8 : grade = 'B';
                break; 
       case  7 : grade = 'C';
                break; 
       case  6 : grade = 'D';
                break; 
       default : grade = 'f';
                break; 
                }// switch
    getch();
    return grade;    
                      }// scoreToGrade

Result:


 Enter the test score (0-100): 89
 The grade is: B

A switch statement in C++



1
2
3
4
5
6
7
8
9
 switch (print flag)
        {
          case 1: cout << " do case 1 \n";
                  doCase1 ();
          case 2: cout << " do case 2 \n";
                  doCase2 ();
          case 3: cout << " do default \n";
                  doDefault ();                     
               } // switch

Write a program of Multivalued case statment in C++


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
   switch (print flag)
    (
     case 1;
     case 3;
             cout << " Good Day\n";
             cout << " Odds have it!\n";
            break;
     case 2;
     case 4;
             cout << " Good Day\n";
             cout << " Even have it!\n";
            break; 
     default:
             cout << " Good Day, I'm confused!\n";
             cout << " Bye!\n";
            break;
                  )// switch