×

Java Programs

Java Practice

Java program to check whether the number is IMEI Number or not

In this java program, we will read a number, which will be an IMEI number of a mobile and check whether it is a valid IMEI number or not.
Submitted by Chandra Shekhar, on January 18, 2018

Problem statement

Given a number, we have to check that the entered number is IMEI Number or not.

An IMEI number is a 15 digit number and it is said to be IMEI number if and only if the sum of the number is exactly divisible by 10. But the condition is that when we entered the number the digit next to the previous one is taken as its double.

Example:

Input: 47415403237518

Output:
47415403237518 – Is an IMEI Number.

Program to check valid IMEI number in java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CheckIMEINumber
{
	// Function for finding and returning sum of digits of a number
	int sumDig(int n) 
	{
		// initialise here.
		int a = 0;
	    while(n>0)
	    {
	    	a = a + n%10;
	        n = n/10;
	    }
	    return a;
	}
	     
	public static void main(String args[])throws IOException
	{
		// create object here.
		CheckIMEINumber ob = new CheckIMEINumber();
	    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	         
	    // The 'Long' data type is used to store 15 digits.
	    System.out.print("Enter a 15 digit IMEI code : ");
	    long n = Long.parseLong(br.readLine()); 
	         
	    // Converting the number into String for finding length
	    String s = Long.toString(n); 
	    int l = s.length();
	    
	    // If length is not 15 then IMEI is Invalid
	    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)
	            {
	        		// Doubling every alternate digit
	        		d = 2*d; 
	            }
	 
	        	// Finding sum of the digits
	        	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

First run:
Enter a 15 digit IMEI code : 111111111111111
Output : Sum = 22
Invalid IMEI Code

Second run:
Enter a 15 digit IMEI code : 474154203237518
Output : Sum = 60
Valid IMEI Code

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.