Programming Examples
Java program for IMEI Number
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.
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.
The IMEI is validated in three steps:
1. Starting from the right, double every other digit (e.g., 7 becomes 14).
2. Sum the digits (e.g., 14 → 1 + 4).
3. Check if the sum is divisible by 10.
Sample 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
Solution
import java.util.*;
class IMEI
{
int sumDig(int n)
{
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();
String s = Long.toString(n);
int l = s.length();
if(l!=15)
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;
}
sum = sum + ob.sumDig(d);
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
Enter a 15 digit IMEI code : 654122487458946
Output : Sum = 80
Valid IMEI Code
Enter a 15 digit IMEI code : 799273987135461
Output : Sum = 79
Invalid IMEI Code
Enter a 15 digit IMEI code : 79927398713
Output : Invalid Input