Pages

Friday, August 23, 2013

Write a program of add two digit in C++

This program extracts and adds the two least significant digits of an integer

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
42
43
44
45
46
47
48
49
50
      #include <iostream.h>
      #include <conio.h>
      using namespace std;
    
    // Prototype Declarations
      int addTwoDigits (int num);
      int firstDigit   (int);
      int secondDigit (int);
  
    int main ()
     {
      cout << " Enter an integer:  ";
      int number;
      cin  >> number;
      
      int sum = addTwoDigits (number);
      cout << " \n Sum of last two digits is:   "<< sum ;
      getch();
      return 0;
               } // main
      /* ============== addTwoDigits ===================
      Adds the first two digits of an integer.
           Pre  num contains an integer 
           Post return sum of two least significant digits
      */         
        int addTwoDigits (int number)       
      {
            int result = firstDigit (number) + secondDigit (number);
            return result;
            } //add two digit
      /* ============== firstDigits ===================
      Extracts the least significant digits of an integer.
           Pre  num contains an integer 
           Post Return least significant digits
      */
      int firstDigit (int num)
      {
          return (num % 10);
          }
      /* ============== secondDigits ===================
      Extracts second least significant digits of an integer.
           Pre  num contains an integer 
           Post Return digit is 10s position                 */    
      int secondDigit (int num)
      {   
          int result = (num / 10) % 10;
      return result;
      getch ();
      return result;
               } //secondDigits  

Result:


 Enter an integer:  23

 Sum of last two digits is:   5

Wednesday, August 21, 2013

Functions

Selection ----- Making Decisions

Structure of C++ Program

Programs:

01.     Write a program of Binary Expression
02.     Write a program that calculate compound assignment
03.     Write a program of Demonstrate Postfix Increment
04.     Write a program of Demonstrate Prefix (unary) Increment 
05.     Write a program of Precedence
06.     Write a program of Evaluating Expressions
07.     Write a program of Implicit type Conversion
08.     Write a program of Explicit Casts
09.     Write a program that Calculate Quotient and Remainder 
10.     Write a program that calculate Average of Four Numbers
11.     Write a program that convert Radians to Degrees
12.     Write a program that calculate Sales Total
13.     Write a program that calculate Student Score
14.     Write a program that Print Rightmost Digit of Integer

Introduction to C++

History of C++ Language

History of C++ Language


C++ Language

                                 "A set of instructions given to computer to
                                 solve some specific task is called a program"

             "C++ is a language in which instructions are written to solve specific task. "  In computer programming world, there are two types of language

1) High Level Language
2) Low Level language /Machine language

High level language:

                                      "A language which is easily understandable for human
                                       is called high level language. C++ is a high level language"


Low level language or Machine language:


                                     "A language which is not understandable for
                                             human is called low level language."

C++ Compiler:

                                  "A compiler is a language translator which 
                          converts High Level Language to Low Level Language"

Refference by Syed Zulqurnain Jaffery & Ms. Shahina Naz

Mathematical Formulas in C++

Formula's use in Science Measurement


01.      Write a program to find Area of a Circle
02.      Write a program to find Area of Ellipse
03.      Write a program to find Area of a Rectangle
04.      Write a program to find Area of a Square 
05.      Write a program to find Area of a Trapezoid
06.      Write a program to find Area of a Triangle                                                                                                                                                                                                                                 
07.      Write a program to find Volume of a Box 
08.      Write a program to find Volume of a cube
09.      Write a program to find Volume of a cylinder 
10.      Write a program to find Volume of a sphere
20.      Write a program to find Volume of a Triangular Prism

21.      Write a program that convert Hour(s) into minutes
22.      Write a program to convert temperature Fahrenheit to Celsius
23.      Write a program to find Energy (Conservation) Formula 
24.      Write a program to find Translation Motion 
25.      Write a program to find Translation motion
26.      Write a program to find the formula of Work

C++ Programming Language

Tuesday, August 20, 2013

Write a program of Nested if statements in C++

Nested if in two-way selection

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
   #include <iostream.h>
   #include <conio.h>
   int main()
   {
       cout << "Please enter two integer: ";
       int a, b;
       cin  >> a >> b ;
   if (a <= b)
      if (a < b)
       cout << a << " <  " << b << endl;
   else // equal
       cout << a << " == " << b << endl;
   else // greater than
       cout << a << " >  " << b << endl;  
       
     getch ();  
     return 0;  
                } //main

Result:


Please enter two integer: 46  79
46 <  79

Write a program of Two-way selection in C++

if...else Statement is a composite statement used to make a decision between two alternatives

 Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
   #include <iostream.h>
   #include <conio.h>
   
   int main()
   {
       cout << " Please enter two integer: ";
       int a, b;
       cin  >> a >> b ;
   if (a <= b)
       cout << a << " <= " << b << endl;
   else
       cout << a << " > "  << b << endl;
     getch ();  
     return 0;  
                } //main

Result:

 Please enter two integer: 23  45
23 <= 45

Write a program of relational operators in C++

Demonstrates the result of Relational operators

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
   #include <iostream.h>
   #include <conio.h>
   #include <iomanip.h>
   int main()
   {
       int a =  5;
       int b = -3;
     cout  << setw(2) << a << " <  " 
           << setw(2) << b << " is "
           << setw(2) << (a < b) << endl;
     cout  << setw(2) << a << " == " 
           << setw(2) << b << " is "
           << setw(2) << (a  ==  b) << endl;
     cout  << setw(2) << a << " != " 
           << setw(2) << b << " is "
           << setw(2) << (a != b) << endl;
     cout  << setw(2) << a << "  > " 
           << setw(2) << b << " is "
           << setw(2) << (a > b) << endl;
     cout  << setw(2) << a << " <= " 
           << setw(2) << b << " is "
           << setw(2) << (a <= b) << endl;
     cout  << setw(2) << a << " >= " 
           << setw(2) << b << " is "
           << setw(2) << (a >= b) << endl;
           getch();
           return 0;
                    } // main

Result:

 5 <  -3 is 0
 5 == -3 is 0
 5 != -3 is 1
 5  > -3 is 1
 5 <= -3 is 0
 5 >= -3 is 1

Write a program of Logical Expressions in C++

In C++ Language
if a value is zero, it can be used as the logical value false.
if a value is not zero, it can be used as the logical value true.

There is three type of logical operator
1.  Not operator ( )
     This unary operator convert true to false and false to true.
2.  And ( && )
     The result of This Binary operator is true only when
     both operator is true; it is false in all other case.
3.  Or ( || )
     The result is false if both oprands are false; its true all other case.

Demonstrates the result of logical operator

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
   #include <iostream.h> 
   #include <conio.h>
   int main()
   {
   cout <<"  5 && - 3 is: " << ( 5 && - 3)<< endl;     
   cout <<"  5 &&   0 is: " << ( 5 &&   0)<< endl; 
   cout <<"  0 &&   5 is: " << ( 0 &&   5)<< endl; 
   cout <<"  5 ||   0 is: " << ( 5 ||   0)<< endl; 
   cout <<"  0 ||   5 is: " << ( 0 ||   5)<< endl; 
   cout <<"  0 ||   0 is: " << ( 0 ||   0)<< endl;     
   cout <<" !5 &&  !0 is: " << (!5 ||  !0)<< endl;     
   cout <<" !5 ||  !0 is: " << (!5 ||  !0)<< endl;     
   cout <<" !5 ||   0 is: " << (!5 ||   0)<< endl; 
   cout <<"  5 ||  !0 is: " << ( 5 ||  !0)<< endl;
   getch();
   return 0;
            } // main

Output:


  5 && - 3 is: 1
  5 &&   0 is: 0
  0 &&   5 is: 0
  5 ||   0 is: 1
  0 ||   5 is: 1
  0 ||   0 is: 0
 !5 &&  !0 is: 0
 !5 ||  !0 is: 0
 !5 ||   0 is: 0
5 || !0 is: 1

Monday, August 19, 2013

Write a program to display Fibonacci series up-to nth term in C++

 A Fibonacci number is a memeber of a set in which each
 number is the sum of the previous two numbers.( The Fibonacci series
 describe a form of a spiral) The series begins
            0, 1, 1, 2, 3, 5, 8, 13, 21, ....
 Write a program that calculate and prints nth term of Fibonacci series.

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
    #include <iostream.h>
    #include <conio.h>
    int main()
    { 
    int n, c, fib1 = 0, fib2 = 1, fib3; 
    cout << " Enter the number of terms of Fibonacci series" << endl;
    cin >> n; 
    cout << "First " << n << " terms of Fibonacci series are: " << endl; 
    for ( c = 0 ; c < n ; c++ )
    {
      if ( c <= 1 )
         fib3 = c;
      else
      {
         fib3 = fib1 + fib2;
         fib1 = fib2;
         fib2 = fib3;
      }
      cout << fib3 << endl;
     }
     getch();
     return 0;        
            } // main

Result:


 Enter the number of terms of Fibonacci series
7
First 7 terms of Fibonacci series are: 
0
1
1
2
3
5
8

Sunday, August 18, 2013

Write a program that that Converts Inches into other measurements in C++

Write a program that converts and prints a user-supplied measurement
 in unches into
1.  Foot  (12 inches)
2.  Centimeter (2.54/inch)
3.  Yard  (36 inches)
4.  Meter (39.37 inches)
5.  Kilometer (100) 

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
   #include <iostream.h>
   #include <conio.h>
   using namespace std;
   int main()
    {
    float inch,in , ft, cm, yrd, m, km;  
    cout << " Enter inche value:  ";
    cin  >> in; 
    ft   = in / 12;
    cm   = in / 0.39370;
    yrd  = in / 36;
    m    = in / 39.37;
    km   = in / 39370;
    cout << " Inches               = " << in  << " Inches \n" ;
    cout << " Inches to Foot       = " << ft  << "  Foot "  << endl;
    cout << " Inches to Centimeter = " << cm  << "  cm "    << endl;  
    cout << " Inches to Yard       = " << yrd << "  Yards " << endl; 
    cout << " Inches to Meter      = " << m   << "  Meter " << endl;
    cout << " Inches to KiloMeter  = " << km  << "  km " << endl;
    getch();
    return 0; 
             }    // main

Result:

 Enter inche value: 4
 Inches               = 4 Inches
 Inches to Foot       = 0.333333  Foot
 Inches to Centimeter = 10.16  cm  
 Inches to Yard       = 0.1111  Yards 
 Inches to Meter      = 0.1016 Meter 
 Inches to KiloMeter  = 0.0001015  km 

Write a program that compute this formula: s^2 + p *(s - x) * (p + y) in C++

Given the following pseudocode, write a program that executes it. Use floating-point types for all values.
read x;
 read y;
 compute p = x * y;
 compute s = x + y;
Total = s^2 + p *(s - x) * (p + y);
 print total.  

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
      #include <iostream.h>
      #include <conio.h>
      int main ()
      {
        float x, y, p, s, s2, Total;  
        cout << " Enter a value of x? \n";
        cin  >> x; 
        cout << " Enter a value of y? \n";  
        cin  >> y;
          p   = x * y;
          s   = x + y;
          s2  = s * s;
        Total = s2 + (p * ((s - x) * (p + y)));
       cout << " s = x + y; p = x * y \n";
       cout << "\n s^2 + (p * ((s - x) * (p + y))) = " << Total ;
       getch ();
       return 0;  
                  }// main

Result:

 Enter a value of x? 
2
 Enter a value of y? 
5
 s = x + y; p = x * y 

 s^2 + (p * ((s - x) * (p + y))) = 7999

Wednesday, August 14, 2013

Write a calculator program in C++

This program add and subtract two integers read from the keyboard

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
    #include <iostream.h>
    #include <conio.h>
    #include <iomanip>
    using namespace std;
    // Prototype Declaration 
    int add  (int a, int b);
    int subt (int a, int b);
    int main ()
    {
        // Prompt user for input and get data
    cout << "\n Enter two integer numbers: ";
    // Read numbers into a and b
    int a, b;
    cin  >> a >> b ;
    // Calculate the sum and difference
    int sum  = add  (a, b);
    int diff = subt (a, b);
    cout << setw(5) << a << "  +  " << b << "  =  " <<  sum  << endl;
    cout << setw(5) << a << "  -  " << b << "  =  " <<  diff << endl;
    // Close Program
    getch();
    return 0;
           } // main
    /* =============== add =====================
       This function adds two integer and returns the sum
       Pre  Parameter a and b
       Post Returns a + b            */  
    int add (int a, int b)
    {
     return (a + b);
                    } // add   
    /* =============== subt =====================
       Returns the difference of two integer
       Pre  Parameter a and b
       Post Returns a - b            */  
    int subt (int a, int b)
    {
     return (a - b);       
                    } // subt

Result:


 Enter two integer numbers: 5  10
    5  +  10  =  15
    5  -  10  =  -5

Write a program which can scaling for random number generation in C++

Demonstrates the generation of random number in three different
ranges:
        03 through 07
        20 through 50
        -6 through 15          

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
    #include <iostream.h>
    #include <conio.h>
    #include <cstdlib>
    using namespace std;
    int main ()
     {
      cout << " Enter random number seed:  ";
      int seed;
      cin  >> seed;
      srand ( seed );
             
      cout << " Random number with seed    " << seed <<" : "
           << rand () << " " << rand () << "  "
           << rand () << " " << rand () << "\n\n\n";
             
      cout << " Enter another random seed:";
      cin  >> seed ;
      srand  ( seed );
             
      cout << " Random numbers with seed  " << seed << " : "
           << rand () << " " << rand () << " "
           << rand () << " " << rand () << "\n\n";
      cout << " Enter another random seed:";
      cin  >> seed ;
      srand ( seed );
             
     cout << " Random numbers with seed  " << seed << " : "
         << rand () << " " << rand () << " "
         << rand () << " " << rand () << "\n\n";
    cout << " Hope you found these numbers intresting!!! :)\n ";
      getch ();
      return 0;
                 } // main

Result:


Range 03 to 07:   5
Range 20 to 50:  27
Range -6 to 15:  -2

Write a program that domenstratr randoom number series in C++

Demonstrates the use of srand to generate random numbers in different series.The user asked to input a seed, and the program then generates four random numbers. The process is  repeated three times

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
    #include <iostream.h>
    #include <conio.h>
    #include <cstdlib>
    using namespace std;
    int main ()
     {
      cout << " Enter random number seed:  ";
      int seed;
      cin  >> seed;
      srand ( seed );
             
      cout << " Random number with seed    " << seed <<" : "
           << rand () << " " << rand () << "  "
           << rand () << " " << rand () << "\n\n\n";
             
      cout << " Enter another random seed:";
      cin  >> seed ;
      srand  ( seed );
             
      cout << " Random numbers with seed  " << seed << " : "
           << rand () << " " << rand () << " "
           << rand () << " " << rand () << "\n\n";
      cout << " Enter another random seed:";
      cin  >> seed ;
      srand ( seed );
             
     cout << " Random numbers with seed  " << seed << " : "
         << rand () << " " << rand () << " "
         << rand () << " " << rand () << "\n\n";
    cout << " Hope you found these numbers intresting!!! :)\n ";
      getch ();
      return 0;
                 } // main

OuTpUt:

 Enter random number seed: 255
 Enter random number seed  255 : 10966  21850 28819 871

  
 Enter another random seed:511    
 Random numbers with seed  511 : 10790 7557 27899 1707

 Enter another random seed: 997    
 Random numbers with seed   997: 7384 6025 8744 3294
 
 Hope you found these numbers intresting!!! :)

Write a program that Demonstrate Default Arguments in C++

Demonstrate use of Parameter Default

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
   #include <iostream.h>
   #include <conio.h>
   using namespace std;
   // Prototype Declarations
   void printInt (int num = 0); // Note default to 0 
   int main()
   {
       // Demonstrate Default Parameter
       cout << " Calling with no Parameter \n";
       printInt ();
       // Demonstrate parameter value passed 
       cout << "\n Calling with parameter 5 \n";
       printInt (5);
       return 0;
       } //main
       /* ============= printInt =================
       Print integer value
       pre  nothing Post Either 0 or parameter 
       value printed       */
       void printInt (int num)
       {
          cout << " Parameter value is: " << num << endl; 
          getch();
          return;
                  }// printInt

OuTpUt:

 Calling with no Parameter
 Parameter value is: 0

 Calling with parameter 5 
 Parameter value is: 0

Tuesday, August 13, 2013

Write a program that display Quotient and Remainder in C++

This program reads twe intgers and then prints the
Quotient and Remainder of the first number divided by second

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
   #include <iostream.h>
   #include <conio.h>
   #include <iomanip.h>
   using namespace std;
   // Prototype Declarations
   void getData  (int& divnd, int& divsr);
   void divide   (int  divnd, int  divsr,
                  int& quot,  int& rem);
   void print    (int  quot,  int  rem);
   
   int main()
   {
       int a;
       int b;
       getData (a, b);
       
       int c;
       int d;
       divide (a, b, c, d);
       print  (c, d);
       getch();
       return 0;
                } // main
    /* ============= getData ======================
     This function reads two numbers into variables
     specified in the parameter list.
       Pre  nothing 
       Post Data read and placed in calling function    */
    void getData (int& divnd, int& divsr)
    {
         cout << " Enter two Intger and Return:   ";
         cin  >> divnd >> divsr;
         return;
         }   //getData
    /* =================== divide ==================
     This function divides two intger and the reminder.
     Quotient / reminder in calling program variable 
      Pre  dividend & divisor contain integer value
      Post quotient & reminder calc'd                  */     
      void divide (int divnd, int divsr,
                   int& quot, int& rem )
    { 
       quot = divnd / divsr;
       rem  = divnd % divsr;
       getch();
       return;                 
                   } // divide
    /* =================== Print ==================
     This function prints the quotient and reminder.  
      Pre  quot contions the quotient
           rem contains the reminder
      Post quotient and reminder printed               */     
    void print (int quot, int rem)
    {
     cout << " Quotient :   ";
     cout << setw(4) << quot << endl;
     cout << " Reminder :   ";
     cout << setw(4) << rem << endl;
     return ;
                } // print   

OuTpUt:


 Enter two Intger and Return:   5678    2134
 Quotient :      2
 Reminder :   1410

Write a program that calculate Strange College Fees in C++

This program prints the tuition at Strange College Fees. Strange Charges $10 for
registration, plus $10 per unit and a penalty of $50 for each 12 units,
 or fraction of 12, over 12.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    #include <iostream.h>
    #include <conio.h>
   using namespace std;
   
   const int regFee      = 10;
   const int unitFee     = 10;
   const int excessFee   = 50;
   // Prototype Declarations
   int calculateFee   (int firstTerm,
                       int secondTerm,
                       int thirdTerm);
   int termFee (int units);
   int main ()
   {
       cout << " Enter units for  First term:     ";
       int firstTerm;
       cin  >> firstTerm;
       
       cout << " Enter units for Second term:     ";
       int secondTerm;
       cin  >> secondTerm;
       
       cout << " Enter units for  Third term:     ";
       int thirdTerm;
       cin  >> thirdTerm;
       
       int totalFee;
       totalFee = calculateFee
                   (firstTerm, secondTerm, thirdTerm);
       cout << "\n The total tuition is :\t" << totalFee;
       getch ();
       return 0;
       }// main
       /* ================ CalculateFee =====================
       Calculate the total fees for the year.
              Pre  The number units to be taken each term 
              Post Return the annual fees            */
       int calculateFee (int firstTerm, 
                         int secondTerm,
                         int thirdTerm)
     { 
           int fee = termFee (firstTerm)
                   + termFee(secondTerm)
                   + termFee (thirdTerm);
      getch ();             
      return fee;
     }// calculationFee
     /* ================ termfees =======================
             Calculate the tuition for one term.
        Pre  units contains units to be taken in the term
        Post the fee is calculated and returned        */
     
     int termFee (int units) 
     {
         int totalFee = regFee
                   + ((units - 1) / 12 * excessFee)
                   +  (units * unitFee);
      
      return (totalFee);
      getch();
      } // termFee   

Result:


 Enter units for  First term:     10
 Enter units for Second term:     20
 Enter units for  Third term:     30 

 The total tuition is :   780

Write a program that Print six digits with commas in C++

This program reads long integers from the keyboard and prints them with leading
zeros in the form 123,456 with a comma between the 3rd & 4th digit

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
   #include <iostream>
   #include <conio.h>
   #include <iomanip.h>
   using namespace std;
   // Prototype declarations
   void printWithComma (long num);
   int main ()
   {
       cout << " Enter a number with upto 6 digits: ";
       long number;
       cin  >> number;
       printWithComma (number);
       return 0; 
              } // main
    /* ============= printWithComma ========================
    This function divides num into two three digit numbers 
    and prints them with a comma inserted
        Pre  num is six-digit number
        Post num has been printed with a comma inserted    */ 
        
    void printWithComma (long num)
    {
         float thousands = num / 1000;
         float hundreds  = num % 1000;
         cout << "\n The number you entered is \t";
         cout << setw(3) << thousands <<" , ";
         cout << setfill('0');
         cout << setw(3) << hundreds;
         getch ();
         return ;
                 }// printWithComma

Result:

 Enter a number with upto 6 digits: 123456

 The number you entered is 123,456