9’s complement of a decimal number is the subtraction of it’s each digits from 9. Like 1’s complement, 9’s complement is used to subtract a number using addition.

For example, let us compute value of “718 – 123” using 9’s complement and addition. We first find 9’s complement of 718 which is 281. Now we add 281 to 123. We get 404. 9’s complement of this is 595 which is equal to “718 – 123”. So we could find subtraction using addition and 9’s complement.

In case while adding a carry is obtained in the end, also known as the end around carry, it should be added to the answer, removing the carry itself. For example, (83-25) , 9’s complement of 25 is 74 and (83+74 = 157). A carry is obtained, now add it to the number 57, (57+1 = 58) which is the answer.

Given a decimal number n, find 9’s complement of the number.

Input : 25 Output : 9's complement is : 74 Input : 345.45 Output : 9's complement is : 654.54

Let the number be stored as string. We traverse digits of number and subtract every digit from 9.

## C++

`// C++ program to find 9's complement of a ` `// number. ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `void` `complement(string number) ` `{ ` ` ` `for` `(` `int` `i=0 ; i < number.length() ; i++ ) ` ` ` `if` `(number[i] != ` `'.'` `) ` ` ` `number[i] = ` `'9'` `- number[i] + ` `'0'` `; ` ` ` ` ` `cout << ` `"9's complement is : "` `<< number; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string number = ` `"345.45"` `; ` ` ` `complement(number); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find 9's complement of a ` `// number. ` ` ` `class` `GFG{ ` `static` `void` `complement(String number1) ` `{ ` ` ` `char` `[] number=number1.toCharArray(); ` ` ` `for` `(` `int` `i=` `0` `; i < number.length ; i++ ) ` ` ` `if` `(number[i] != ` `'.'` `) ` ` ` `number[i] = (` `char` `)((` `int` `)(` `'9'` `) - (` `int` `)(number[i]) + (` `int` `)(` `'0'` `)); ` ` ` `System.out.println( ` `"9's complement is : "` `+String.valueOf(number)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String number = ` `"345.45"` `; ` ` ` `complement(number); ` `} ` `} ` `//This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find 9's ` `# complement of a number. ` ` ` `def` `complement(number): ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(number)): ` ` ` `if` `(number[i] !` `=` `'.'` `): ` ` ` `a ` `=` `9` `-` `int` `(number[i]) ` ` ` `number ` `=` `(number[:i] ` `+` ` ` `str` `(a) ` `+` `number[i ` `+` `1` `:]) ` ` ` ` ` `print` `(` `"9's complement is : "` `, number) ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `number ` `=` `"345.45"` ` ` `complement(number) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find 9's complement of a ` `// number. ` `using` `System; ` ` ` `class` `GFG{ ` `static` `void` `complement(` `string` `number1) ` `{ ` ` ` `char` `[] number=number1.ToCharArray(); ` ` ` `for` `(` `int` `i=0 ; i < number.Length ; i++ ) ` ` ` `if` `(number[i] != ` `'.'` `) ` ` ` `number[i] = (` `char` `)((` `int` `)(` `'9'` `) - ` ` ` `(` `int` `)(number[i]) + (` `int` `)(` `'0'` `)); ` ` ` `System.Console.WriteLine( ` `"9's complement is : "` ` ` `+` `new` `string` `(number)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `String number = ` `"345.45"` `; ` ` ` `complement(number); ` `} ` `} ` `//This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find 9's complement of a ` `// number. ` ` ` `function` `complement( ` `$number` `) ` `{ ` ` ` `for` `( ` `$i` `=0 ; ` `$i` `< ` `strlen` `(` `$number` `) ; ` `$i` `++ ) ` ` ` `if` `(` `$number` `[` `$i` `] != ` `'.'` `) ` ` ` `$number` `[` `$i` `] = ` `'9'` `- ` `$number` `[` `$i` `] + ` `'0'` `; ` ` ` ` ` `echo` `"9's complement is : "` `, ` `$number` `; ` `} ` ` ` `// Driver code ` ` ` `$number` `= ` `"345.45"` `; ` ` ` `complement(` `$number` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

9's complement is : 654.54

This article is contributed by **Dibyendu Roy Chaudhuri**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- What’s difference between 1's Complement and 2's Complement?
- 10's Complement of a decimal number
- Convert Decimal To Hexa-Decimal including negative numbers
- 1's and 2's complement of a Binary Number
- Check if binary representation of a given number and its complement are anagram
- 8085 program to find 1's and 2's complement of 8-bit number
- 8085 program to find 1’s and 2’s complement of 16-bit number
- Previous number same as 1's complement
- Interface 8255 with 8085 microprocessor for 1’s and 2’s complement of a number
- Complement of a number with any base b
- Efficient method for 2's complement of a binary string
- Check if one of the numbers is one's complement of the other
- Find One's Complement of an Integer
- Find relative complement of two sorted arrays
- 8085 program to find 2's complement of the contents of Flag Register
- Check if bits in range L to R of two numbers are complement of each other or not
- Why are negative numbers stored as 2's complement?
- Subtraction of two numbers using 2's Complement
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
- Count number of digits after decimal on dividing a number