Friday, February 10, 2017

Write a program in Java to input the first 14 digits of an IMEI number and find the (check)last digit of it.


The IMEI  has 14 digits plus a check digit) includes information on the origin, model, and serial number
of the device. The check digit (x) is obtained by computing the sum of digits then multiply that sum
with 9. And do modulo by 10. The modulo result will be the last(check) digit.

Write a program in Java to input the first 14 digits of an IMEI number and find the (check)last digit of it.

Ex:- IMEI No  49015420323751x
Step 1 :- Compute the sum of the digits according to the rule of IMEI Addition(52 in this case).
Step 2 :- Multiply the sum by 9 (9*52 = 468).
Step 3 :- Divide the result by 10 and note down the remainder (468 % 10)
Step 4 :- The last digit, 8, is the check digit.
 */

import static java.lang.System.out;
import java.io.Console;
class Imei2
{
static public int sumDigit(int rem)
{
int s=0;
while(rem!=0)
{
s=s+rem%10;
rem/=10;
}
return s;
}
public static void main(String...alt)
{
Console c=System.console();
String s=c.readLine("Enter 14 digit of IMEI Number : ");
long n=Long.parseLong(s);
if(s.length()!=14)
out.println("Wrong Imei inserted ");
else
{
int sum=0,rem=0;
for(int i=14;i>=1;i--)
{
rem=(int)(n%10);
if(i%2==0)
rem=2*rem;
sum=sum+sumDigit(rem);
n/=10;
}
out.println("Sum of IMEI = "+sum);
int ld=(sum*9)%10; //Finding Last digit
out.println("Last digit of "+s+" = "+ld);
}
}
}

Share this

0 Comment to " Write a program in Java to input the first 14 digits of an IMEI number and find the (check)last digit of it."

Post a Comment