10’s Complement of a decimal number?

CServer Side ProgrammingProgramming

9’s complement and 10’s complement are used to make the arithmetic operations in digital system easier. These are used to make computational operations easier using complement implementation and usually trade hardware usage to the program.

To obtain the 9’s complement of any number we have to subtract the number with (10n – 1) where n = number of digits in the number, or in a simpler manner we have to subtract each digit of the given decimal number from 9.

10’s complement, it is relatively easy to find out the 10’s complement after finding out the 9’s complement of that number. We have to add 1 with the 9’s complement of any number to obtain the desired 10’s complement of that number. Or if we want to find out the 10’s complement directly, we can do it by following the following formula, (10n – number), where n = number of digits in the number.

Let us take a decimal number 456, 9’s complement of this number will be

999
-456
_____
543

10’s complement of this no

543
(+)1
______
544


Input:456
Output:544

Explanation

Mathematically,

10’s complement = 9’s complement + 1
10’s complement = 10i – num

Where, i = total number of digits in num.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main() {
   int i=0,temp,comp,n;
   n=456;
   temp = n;
   while(temp!=0) {
      i++;
      temp=temp/10;
   }
   comp = pow(10,i) - n;
   cout<<comp;
   return 0;
}
raja
Published on 19-Aug-2019 12:46:32
Advertisements