给定十进制数字N。任务是找到数字N的10的补码。
例子:
Input : 25
Output : 10's complement is : 75
Input : 456
Output : 10's complement is : 544
十进制数字的10补码可以通过在该十进制数字的9补码上加1来找到。就像二进制数字表示中的2s补语一样。
数学上
10’s complement = 9’s complement + 1
例如,让我们取一个十进制数456,此数字的9的补码将是999-456,即543。现在,10的补码将是543 + 1 = 544。
所以,
10’s complement = 10len – num
下面是查找给定数字的10的补码的程序:
C++
Where, len = total number of digits in num.
Java
// C++ program to find 10's complement
#include
#include
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<
Python3
// 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.
C#
# 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
PHP
// 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.
输出: