Java Program to check for Valid IMEI Number
Java program to accept a fifteen digit number from the user and check whether it is a valid IMEI number or not.
Question:
The International Mobile Station Equipment Identity or IMEI is a number, usually unique, to identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery compartment of the phone.
The IMEI number is used by a GSM network to identify valid devices and therefore can be used for stopping a stolen phone from accessing that network. The IMEI (15 decimal digits: 14 digits plus a check digit) includes information on the origin, model, and serial number of the device.See: Java Program to find check digit of an IMEI Number
The IMEI is validated in three steps:- Starting from the right, double every other digit (e.g., 7 becomes 14).
- Sum the digits (e.g., 14 → 1 + 4).
- Check if the sum is divisible by 10.
If input is IMEI = 490154203237518
Since, 60 is divisible by 10, hence the given IMEI number is Valid. Design a program to accept a fifteen digit number from the user and check whether it is a valid IMEI number or not. For an invalid input, display an appropriate message.Programming Code:
Java
/**
* The class IMEI inputs a 15 digit number and checks whether it is a valid IMEI or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class IMEI
{
int sumDig(int n) // Function for finding and returning sum of digits of a number
{
int a = 0;
while(n>0)
{
a = a + n%10;
n = n/10;
}
return a;
}
public static void main(String args[])
{
IMEI ob = new IMEI();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a 15 digit IMEI code : ");
long n = sc.nextLong(); // 15 digits cannot be stored in 'int' data type
String s = Long.toString(n); // Converting the number into String for finding length
int l = s.length();
if(l!=15) // If length is not 15 then IMEI is Invalid
System.out.println("Output : Invalid Input");
else
{
int d = 0, sum = 0;
for(int i=15; i>=1; i--)
{
d = (int)(n%10);
if(i%2 == 0)
{
d = 2*d; // Doubling every alternate digit
}
sum = sum + ob.sumDig(d); // Finding sum of the digits
n = n/10;
}
System.out.println("Output : Sum = "+sum);
if(sum%10==0)
System.out.println("Valid IMEI Code");
else
System.out.println("Invalid IMEI Code");
}
}
}Output:
1. Enter a 15 digit IMEI code : 654122487458946 Output : Sum = 80 Valid IMEI Code
2. Enter a 15 digit IMEI code : 799273987135461 Output : Sum = 79 Invalid IMEI Code
3. Enter a 15 digit IMEI code : 79927398713 Output : Invalid Input
