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

No comments:

Post a Comment