Tuesday 30 September 2014

• Complements • Subtraction using complements.



• Complements
• Subtraction using complements.

By the end of this class you should be able to:

• Obtain the r’s and (r-1)’s complements in decimal, binary,
and any other number system
• Perform subtraction by addition of the complements


Decimal number complements:

9’s complement of the decimal number N = (10n
 – 1) – N
= n (9’s) – N
i.e. {subtract each digit from 9}

Example Æ 9’s complement of 134795 is 865204
Similarly

1’s complement of the binary number N = (2n
 -1) – N = n (1’s) – N
Example Æ 1’s complement of 110100101 is 001011010
which can be obtained by replacing each one by a zero and each
zero by one.

r’s complement: EE200(class 2-2) Prof. M.M. Dawoud 2of 5
10’s complement of the decimal number N = 10n
 – N = (r-1)’s
complement + 1

Example Æ 10’s complement of 134795 is 865205


Example Æ find the 9’s and 10’s complements of 314700.

Answer Æ 9’s complement = 685299
 10’s complement=685300

Rule: To find the 10’s complement of a decimal number leave all
leading zeros unchanged. Then subtract the first non-zero digit
from 10 and all the remaining digits from 9’s.

The 2’s complement of a binary number is defined in a similar
way.

Example: Find the 1’s and 2’s complements of the binary
number 1101001101

Answer Æ 1’s complement is 0010110010
 2’s complement is 0010110011

Example: Find the 1’s and 2’s complements of 100010100

Answer Æ 1’s complement is 011101011
 2’s complement is 011101100

Subtraction using r’s complement:

To find M-N in base r, we add M + r’s complement of N

Result is M + (rn
 – N)

1) If M > N then result is M – N + rn (rn is an end carry and
can be neglected.
 EE200(class 2-2) Prof. M.M. Dawoud 3of 5
2) If M < N then result is rn
 –(N-M) which is the r’s complement
of the result.

Example: Subtract (76425 – 28321) using 10’s complements.


Answer Æ 10’s complement of 28321 is 71679

 Then add Æ 7 6 4 2 5
 + 7 1 6 7 9

 1 4 8 1 0 4

Therefore the difference is 48104 after discarding the end carry.


Example: subtract (28531 – 345920)

Answer Æ It is obvious that the difference is negative. We
also have to work with the same number of digits, when dealing
with complements.

 10’s complement of 345920 is 654080

 Then add Æ 0 2 8 5 3 1
 + 6 5 4 0 8 0

 6 8 2 6 1 1
Therefore the difference is negative and is equal to the 10’s
complement of the answer.
 Difference is Æ - 317389

The same rules apply to binary.

Example: subtract (11010011 – 10001100)

Discard
No end carry EE200(class 2-2) Prof. M.M. Dawoud 4of 5
Answer Æ 2’s complement of 10001100 is 01110100

Then add Æ 1 1 0 1 0 0 1 1
 + 0 1 1 1 0 1 0 0

 1 0 1 0 0 0 1 1 1
The difference is positive and is equal to 01000111

The same rules apply to subtraction using the (r-1)’s
complements. The only difference is that when an end carry is
generated, it is not discarded but added to the least significant
digit of the result. Also, if no end carry is generated, then the
answer is negative and in the (r-1)’s complement form.


Example: Subtract (76425 – 28321) using 9’s complements.
Answer Æ 9’s complement of 28321 is 71678

 Then add Æ 7 6 4 2 5
 + 7 1 6 7 8

 1 4 8 1 0 3
 1
 4 8 1 0 4



Example: subtract (11010011 – 10001100) using 1’s complement.

Answer Æ 1’s complement of 10001100 is 01110011

Then add Æ 1 1 0 1 0 0 1 1
 + 0 1 1 1 0 0 1 1

 1 0 1 0 0 0 1 1 0
 1

Discard
Difference
Difference EE200(class 2-2) Prof. M.M. Dawoud 5of 5
 1 0 1 0 0 0 1 1 1 

Tuesday 23 September 2014

Make a simple currency converter for 3 currencies



Question :  You are required to write a program for Currency Exchange rates. The basic idea is that user/reader will be able to interchange different currencies using our program. User will have three options i.e. Pakistani Rupees, US Dollars, and Euro. In the end of the program, user should be asked if he/she wants to make another conversion. If user presses y then the whole program should start again. If user presses n then the program should terminate.



 #include <iostream>  
 using namespace std;  
 void main()  
 {  
      int amount=0;  
      char choice1, choice2, choice3='Y';  
      while (choice3=='Y')  
      {  
           cout<<"Select the currencies that you want to exchange\n";  
           cout<<" - Enter R for Pakistani Rupees\n";  
           cout<<" - Enter E for Euro\n";  
           cout<<" - Enter D for Dollar\n"<<endl;  
           cout<<"Please select the currency that you want to convert"<<endl;  
           cin>>choice1;  
           cout<<"Please enter the currency that you want to convert into"<<endl;  
           cin>>choice2;  
           cout<<"Enter Amount: ";  
           cin>>amount;  
           if ( choice1=='R' && choice2=='E')  
           {  
                //division of input rupees by 112 to convert into euros  
                cout<<amount <<" Rupees = "<<amount/112<<" Euros"<<endl;    
           }  
           if ( choice1=='R' && choice2=='D')  
           {  
                //division of input rupees by 84 to convert into dollars  
                cout<<amount <<" Rupees = "<<amount/84<<" Dollars"<<endl;   
           }  
           if ( choice1=='E' && choice2=='R')  
           {  
                //multiplication of input Euros by 112 to convert into rupees  
                cout<<amount <<" Euros = "<<amount*112<<" Rupees"<<endl;   
           }  
           if ( choice1=='E' && choice2=='D')  
           {  
                //multiplication of input Euros by 1.33 to convert into dollars  
                cout<<amount <<" Euros = "<<amount*1.33<<" Dollars"<<endl;   
           }  
           if ( choice1=='D' && choice2=='R')  
           {  
                //multiplication of input dollars by 84 to convert into rupees  
                cout<<amount <<" Dollars = "<<amount*84<<" Rupees"<<endl;   
           }  
           if ( choice1=='D' && choice2=='E')  
           {  
                //division of input dollars by 1.33 to convert into euros  
                cout<<amount <<" Dollars = "<<amount/1.33<<" Euros"<<endl;   
           }  
           cout<<"Do you want to make another conversion? ( Y/N ) :";  
           cin>>choice3;  
      }  
      cout<<"Goodbye :) "<<endl;  
 }