Dado un número decimal N. La tarea es encontrar el complemento a 10 del número N.
Ejemplo:
Input : 25 Output : 10's complement is : 75 Input : 456 Output : 10's complement is : 544
El complemento a 10 de un número decimal se puede encontrar sumando 1 al complemento a 9 de ese número decimal . Es como el complemento a 2 en la representación de números binarios.
Matemáticamente,
complemento a 10 = complemento a 9 + 1
Por ejemplo, tomemos un número decimal 456, el complemento de 9 de este número será 999-456 que será 543. Ahora el complemento de 10 será 543+1=544.
Por lo tanto,
Complemento a 10 = 10 len – num
Where, len = total number of digits in num.
A continuación se muestra el programa para encontrar el complemento a 10 de un número dado:
C++
// C++ program to find 10's complement #include<iostream> #include<cmath> using namespace std; // Function to find 10's complement int 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 code int main() { cout<<complement(25)<<endl; cout<<complement(456); return 0; }
Java
// Java program to find 10's complement import java.io.*; class GFG { // Function to find 10's complement static 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 code public static void main (String[] args) { System.out.println(complement(25)); System.out.println(complement(456)); } } // This code is contributed // by chandan_jnu.
Python3
# Python3 program to find # 10's complement import math # Function to find 10's complement def 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 code print(complement(25)); print(complement(456)); # This code is contributed by mits
C#
// C# program to find // 10's complement using System; class GFG { // Function to find 10's complement static 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 code public static void Main () { Console.WriteLine(complement(25)); Console.WriteLine(complement(456)); } } // This code is contributed // by chandan_jnu.
PHP
<?php // PHP program to find 10's complement // Function to find 10's complement function 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 code echo complement(25) . "\n"; echo complement(456); // This code is contributed by mits ?>
Javascript
<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
Publicación traducida automáticamente
Artículo escrito por Ayusharma0698 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA