# Complement of a number with any base b

In this post, a general method of finding complement with any arbitrary base is discussed.

Steps to find (b-1)’s complement: To find (b-1)’s complement,

• Subtract each digit of the number from the largest number in the number system with base .
• For example, if the number is a three digit number in base 9, then subtract the number from 888 as 8 is the largest number in base 9 number system.
• The obtained result is the (b-1)’s (8’s complement) complement.

Steps to find b’s complement: To find b’s complement, just add 1 to the calculated (b-1)’s complement.

Now this holds true for any base in the number system that exists. It can be tested with familiar bases that is the 1’s and 2’s complement.

Example:

```Let the number be 10111 base 2 (b=2)
Then, 1's complement will be 01000 (b-1)
2's complement will be 01001 (b)

Taking a number with Octal base:
Let the number be -456.
Then 7's compliment will be 321
and 8's compliment will be 322
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Below is the implementation of above idea:

## C++

 `// CPP program to find complement of a  ` `// number with any base b ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to find (b-1)'s complement ` `int` `prevComplement(``int` `n, ``int` `b) ` `{ ` `    ``int` `maxDigit, maxNum = 0, digits = 0, num = n; ` `     `  `    ``// Calculate number of digits  ` `    ``// in the given number ` `    ``while``(n!=0) ` `    ``{ ` `        ``digits++; ` `        ``n = n/10; ` `    ``} ` `     `  `    ``// Largest digit in the number ` `    ``// system with base b ` `    ``maxDigit = b-1; ` `     `  `    ``// Largest number in the number ` `    ``// system with base b ` `    ``while``(digits--) ` `    ``{ ` `        ``maxNum = maxNum*10 + maxDigit; ` `    ``} ` `     `  `    ``// return Complement ` `    ``return` `maxNum - num; ` `} ` ` `  `// Function to find b's complement ` `int` `complement(``int` `n, ``int` `b) ` `{    ` `    ``// b's complement = (b-1)'s complement + 1 ` `    ``return` `prevComplement(n,b) + 1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``cout << prevComplement(25, 7)<

## Java

 `// Java program to find complement  ` `// of a number with any base b ` `class` `GFG ` `{ ` ` `  `// Function to find (b-1)'s complement ` `static` `int` `prevComplement(``int` `n, ``int` `b) ` `{ ` `    ``int` `maxDigit, maxNum = ``0``,  ` `        ``digits = ``0``, num = n; ` `     `  `    ``// Calculate number of digits  ` `    ``// in the given number ` `    ``while``(n != ``0``) ` `    ``{ ` `        ``digits++; ` `        ``n = n / ``10``; ` `    ``} ` `     `  `    ``// Largest digit in the number ` `    ``// system with base b ` `    ``maxDigit = b - ``1``; ` `     `  `    ``// Largest number in the number ` `    ``// system with base b ` `    ``while``((digits--) > ``0``) ` `    ``{ ` `        ``maxNum = maxNum * ``10` `+ maxDigit; ` `    ``} ` `     `  `    ``// return Complement ` `    ``return` `maxNum - num; ` `} ` ` `  `// Function to find b's complement ` `static` `int` `complement(``int` `n, ``int` `b) ` `{  ` `    ``// b's complement = (b-1)'s ` `    ``// complement + 1 ` `    ``return` `prevComplement(n, b) + ``1``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``System.out.println(prevComplement(``25``, ``7``)); ` `     `  `    ``System.out.println(complement(``25``, ``7``)); ` `} ` `} ` ` `  `// This code is contributed ` `// by Kirti_Mangal `

## Python 3

 `# Python 3 program to find  ` `# complement of a number ` `# with any base b ` ` `  `# Function to find  ` `# (b-1)'s complement  ` `def` `prevComplement(n, b) : ` `    ``maxNum, digits, num ``=` `0``, ``0``, n ` ` `  `    ``# Calculate number of digits  ` `    ``# in the given number  ` `    ``while` `n > ``1` `: ` `        ``digits ``+``=` `1` `        ``n ``=` `n ``/``/` `10` ` `  `    ``# Largest digit in the number  ` `    ``# system with base b  ` `    ``maxDigit ``=` `b ``-` `1` ` `  `    ``# Largest number in the number  ` `    ``# system with base b  ` `    ``while` `digits : ` `        ``maxNum ``=` `maxNum ``*` `10` `+` `maxDigit ` `        ``digits ``-``=` `1` `         `  `    ``# return Complement  ` `    ``return` `maxNum ``-` `num ` ` `  `# Function to find b's complement ` `def` `complement(n, b) : ` ` `  `    ``# b's complement = (b-1)'s ` `    ``# complement + 1  ` `    ``return` `prevComplement(n, b) ``+` `1` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``# Function calling ` `    ``print``(prevComplement(``25``, ``7``)) ` `    ``print``(complement(``25``, ``7``)) ` ` `  `# This code is contributed  ` `# by ANKITRAI1 `

## C#

 `// C# program to find complement  ` `// of a number with any base b ` `class` `GFG ` `{ ` ` `  `// Function to find (b-1)'s complement ` `static` `int` `prevComplement(``int` `n, ``int` `b) ` `{ ` `    ``int` `maxDigit, maxNum = 0,  ` `        ``digits = 0, num = n; ` `     `  `    ``// Calculate number of digits  ` `    ``// in the given number ` `    ``while``(n != 0) ` `    ``{ ` `        ``digits++; ` `        ``n = n / 10; ` `    ``} ` `     `  `    ``// Largest digit in the number ` `    ``// system with base b ` `    ``maxDigit = b - 1; ` `     `  `    ``// Largest number in the number ` `    ``// system with base b ` `    ``while``((digits--) > 0) ` `    ``{ ` `        ``maxNum = maxNum * 10 + maxDigit; ` `    ``} ` `     `  `    ``// return Complement ` `    ``return` `maxNum - num; ` `} ` ` `  `// Function to find b's complement ` `static` `int` `complement(``int` `n, ``int` `b) ` `{  ` `    ``// b's complement = (b-1)'s ` `    ``// complement + 1 ` `    ``return` `prevComplement(n, b) + 1; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``System.Console.WriteLine(prevComplement(25, 7)); ` `     `  `    ``System.Console.WriteLine(complement(25, 7)); ` `} ` `} ` ` `  `// This code is contributed ` `// by mits `

## PHP

 ` `

Output:

```41
42
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.