检查有效 IMEI 号码的程序
国际移动设备识别码 (IMEI)是一个数字,通常是唯一的,用于识别移动电话以及一些卫星电话。它通常印在手机的电池盒内,但也可以通过在拨号盘上输入 *#06# 或在智能手机操作系统的设置菜单中与其他系统信息一起显示在大多数手机的屏幕上。 IMEI 号码被 GSM 网络用于识别有效设备,因此可用于阻止被盗手机访问该网络。
资料来源:维基百科
IMEI(15 位十进制数字:14 位加上一个校验位)包括有关设备来源、型号和序列号的信息。
通过以下步骤验证 IMEI:
- 从最右边的数字开始,每隔一个数字将值加倍(例如,7 变为 14)。
- 如果一个数字加倍得到一个两位数,即大于 9(例如,7 × 2 = 14),则将乘积的数字相加(例如,14:1 + 4 = 5),得到一位数.
- 现在取所有数字的总和。
- 检查总和是否可被 10 整除,即(总模 10 等于 0)则 IMEI 号有效;否则无效。
例子:
Input IMEI : 490154203237518
Output : Since, 60 is divisible by 10, hence the given IMEI number is Valid.
C++
// C++ program to check whether the
// given EMEI number is valid or not.
#include
using namespace std;
// Function for finding and returning
// sum of digits of a number
int sumDig(int n)
{
int a = 0;
while (n > 0)
{
a = a + n % 10;
n = n / 10;
}
return a;
}
bool isValidIMEI(long n)
{
// Converting the number into
// String for finding length
string s = to_string(n);
int len = s.length();
if (len != 15)
return false;
int sum = 0;
for(int i = len; i >= 1; i--)
{
int d = (int)(n % 10);
// Doubling every alternate digit
if (i % 2 == 0)
d = 2 * d;
// Finding sum of the digits
sum += sumDig(d);
n = n / 10;
}
return (sum % 10 == 0);
}
// Driver code
int main()
{
// 15 digits cannot be stored
// in 'int' data type
long n = 490154203237518L;
if (isValidIMEI(n))
cout << "Valid IMEI Code";
else
cout << "Invalid IMEI Code";
return 0;
}
// This code is contributed by Yash_R
Java
// Java program to check whether the
// given EMEI number is valid or not.
import java.io.*;
class IMEI
{
// Function for finding and returning
// sum of digits of a number
static int sumDig(int n)
{
int a = 0;
while (n > 0)
{
a = a + n % 10;
n = n / 10;
}
return a;
}
static boolean isValidIMEI(long n)
{
// Converting the number into String
// for finding length
String s = Long.toString(n);
int len = s.length();
if (len != 15)
return false;
int sum = 0;
for (int i = len; i >= 1; i--)
{
int d = (int)(n % 10);
// Doubling every alternate digit
if (i % 2 == 0)
d = 2 * d;
// Finding sum of the digits
sum += sumDig(d);
n = n / 10;
}
return (sum % 10 == 0);
}
// Driver code
public static void main(String args[]) throws IOException
{
// 15 digits cannot be stored in 'int' data type
long n = 490154203237518L;
if (isValidIMEI(n))
System.out.println("Valid IMEI Code");
else
System.out.println("Invalid IMEI Code");
}
}
Python3
# Python3 code to check whether the
# given EMEI number is valid or not
# Function for finding and returning
# sum of digits of a number
def sumDig( n ):
a = 0
while n > 0:
a = a + n % 10
n = int(n / 10)
return a
# Returns True if n is valid EMEI
def isValidEMEI(n):
# Converting the number into
# Sting for finding length
s = str(n)
l = len(s)
# If length is not 15 then IMEI is Invalid
if l != 15:
return False
d = 0
sum = 0
for i in range(15, 0, -1):
d = (int)(n % 10)
if i % 2 == 0:
# Doubling every alternate digit
d = 2 * d
# Finding sum of the digits
sum = sum + sumDig(d)
n = n / 10
return (sum % 10 == 0)
# Driver code
n = 490154203237518
if isValidEMEI(n):
print("Valid IMEI Code")
else:
print("Invalid IMEI Code")
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to check whether the
// given EMEI number is valid or not.
using System;
class GFG {
// Function for finding and
// returning sum of digits
// of a number
static int sumDig(int n)
{
int a = 0;
while (n > 0)
{
a = a + n % 10;
n = n / 10;
}
return a;
}
static Boolean isValidIMEI(long n)
{
// Converting the number into
// String for finding length
String s = n.ToString();
int len = s.Length;
if (len != 15)
return false;
int sum = 0;
for (int i = len; i >= 1; i--)
{
int d = (int)(n % 10);
// Doubling every alternate
// digit
if (i % 2 == 0)
d = 2 * d;
// Finding sum of the digits
sum += sumDig(d);
n = n / 10;
}
return (sum % 10 == 0);
}
// Driver code
public static void Main()
{
// 15 digits cannot be stored in
// 'int' data type
long n = 490154203237518L;
if (isValidIMEI(n))
Console.Write("Valid IMEI Code");
else
Console.Write("Invalid IMEI Code");
}
}
// This code is contributed by parashar.
Javascript
输出:
Valid IMEI Code