10's Complement of a decimal number - GeeksforGeeks

07 Jul, 2021
Given a decimal number N. The task is to find 10’s complement of the number N.Example:  
Input : 25
Output : 10's complement is : 75

Input : 456
Output : 10's complement is : 544
 
10’s complement of a decimal number can be found by adding 1 to the 9’s complement of that decimal number. It is just like 2s complement in binary number representation.Mathematically,  
10’s complement = 9’s complement + 1  
For example, let us take a decimal number 456, 9’s complement of this number will be 999-456 which will be 543. Now 10s complement will be 543+1=544.Therefore,  
10’s complement = 10len – num  
Where, len = total number of digits in num.
 
Below is the program to find 10’s complement of a given number:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find 10's complement #include<iostream>#include<cmath> using namespace std; // Function to find 10's complementint complement(int num){    int i,len=0,temp,comp;         // Calculating total digits    // in num    temp = num;    while(1)    {        len++;        num=num/10;        if(abs(num)==0)            break;           }         // restore num    num = temp;         // calculate 10's complement    comp = pow(10,len) - num;         return comp;} // Driver codeint main(){    cout<<complement(25)<<endl;         cout<<complement(456);         return 0;}
// Java program to find 10's complementimport java.io.*; class GFG{// Function to find 10's complementstatic int complement(int num){    int i, len = 0, temp, comp;         // Calculating total    // digits in num    temp = num;    while(true)    {        len++;        num = num / 10;        if(Math.abs(num) == 0)            break;    }         // restore num    num = temp;         // calculate 10's complement    comp = (int)Math.pow(10,len) - num;         return comp;} // Driver codepublic static void main (String[] args){    System.out.println(complement(25));         System.out.println(complement(456));}} // This code is contributed// by chandan_jnu.
# Python3 program to find# 10's complementimport math # Function to find 10's complementdef complement(num):    i = 0;    len = 0;    comp = 0;         # Calculating total    # digits in num    temp = num;    while(1):        len += 1;        num = int(num / 10);        if(abs(num) == 0):            break;         # restore num    num = temp;         # calculate 10's complement    comp = math.pow(10, len) - num;         return int(comp); # Driver codeprint(complement(25));print(complement(456)); # This code is contributed by mits
// C# program to find// 10's complementusing System; class GFG{// Function to find 10's complementstatic int complement(int num){    int len = 0, temp, comp;         // Calculating total    // digits in num    temp = num;    while(true)    {        len++;        num = num / 10;        if(Math.Abs(num) == 0)            break;    }         // restore num    num = temp;         // calculate 10's complement    comp = (int)Math.Pow(10, len) - num;         return comp;} // Driver codepublic static void Main (){    Console.WriteLine(complement(25));         Console.WriteLine(complement(456));}} // This code is contributed// by chandan_jnu.
<?php// PHP program to find 10's complement // Function to find 10's complementfunction complement($num){    $i;    $len = 0;    $comp;         // Calculating total    // digits in num    $temp = $num;    while(1)    {        $len++;        $num = (int)($num / 10);        if(abs($num) == 0)            break;    }         // restore num    $num = $temp;         // calculate 10's complement    $comp = pow(10, $len) - $num;         return $comp;} // Driver codeecho complement(25) . "\n";echo complement(456); // This code is contributed by mits?>
<script>// javascript program to find 10's complement    // Function to find 10's complement    function complement(num) {        var i, len = 0, temp, comp;         // Calculating total        // digits in num        temp = num;        while (true) {            len++;            num = parseInt(num / 10);            if (Math.abs(num) == 0)                break;        }         // restore num        num = temp;         // calculate 10's complement        comp = parseInt( Math.pow(10, len) - num);         return comp;    }     // Driver code             document.write(complement(25)+"<br/>");         document.write(complement(456)); // This code contributed by umadevi9616</script>
75
544
 
Chandan_Kumar
Mithun Kumar
umadevi9616
ruhelaa48
complement
math
Numbers
school-programming
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Program for factorial of a number
Print all possible combinations of r elements in a given array of size n
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Find minimum number of coins that make a given value