Program To Check For A Valid Imei Number In C++ - C++ Programming Tutorial
C++ Course / C++ Programs / Program To Check For A Valid Imei Number In C++

Program To Check For A Valid Imei Number In C++

BLUF: Mastering Program To Check For A Valid Imei Number In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Program To Check For A Valid Imei Number In C++

C++ is renowned for its efficiency. Learn how Program To Check For A Valid Imei Number In C++ enables low-level control and high-performance computing in the tutorial below.

Overview

The International Mobile Equipment Identity (IMEI) consists of a 15-digit code that is assigned to each individual mobile device. Its primary purpose is to uniquely identify devices and prevent the usage of stolen or unauthorized phones. A legitimate IMEI number adheres to the Luhn algorithm, which is a methodical process used for verifying various forms of identification numbers. This tutorial will delve into a C++ script designed to verify the authenticity of an IMEI number.

IMEI Validation Algorithm:

The IMEI number is validated using the Luhn algorithm, also known as the "modulus 10" or "mod 10" algorithm. Here is how it works:

  • Double every second digit from the right.
  • If doubling a digit results in a number greater than 9, subtract 9 from the result.
  • Sum up all the digits.
  • Check if the total is divisible by 10. If it is, the IMEI is valid.
  • C++ Implementation

Let's consider an example to demonstrate how to validate an IMEI Number in C++.

Example

#include <iostream>
#include <string>
#include <cctype>
// Function to check if a string consists of only digits
bool isNumeric(const std::string& str) 
{
    for (char c : str) 
{
        if (!isdigit(c)) 
{
            return false;
        }
    }
    return true;
}
// Function to validate the IMEI using the Luhn algorithm
bool isValidIMEI(const std::string& imei) 
{
    if (imei.length() != 15) 
{
return false; // IMEI must be 15 digits long
    }    
    if (!isNumeric(imei)) 
{
 return false; // IMEI should only contain numeric characters
    }
    int sum = 0;
    for (int i = 0; i < 15; ++i) 
{
  int digit = imei[i] - '0'; // Convert char to integer
 // Double every second digit from the right
 if (i % 2 == 1) 
{
digit *= 2;
if (digit > 9) 
{
 digit -= 9; // If the result is greater than 9, subtract 9
}
}
sum += digit;
}
// The IMEI is valid if the sum is divisible by 10
return (sum % 10 == 0);
}
int main() 
{
std::string imei;
std::cout << "Enter the 15-digit IMEI number: ";
std::cin >> imei;
if (isValidIMEI(imei)) 
{
std::cout << "The IMEI number is valid.\n";
}
 else 
{
std::cout << "The IMEI number is invalid.\n";
}
    return 0;
}

Output:

If the total is a multiple of 10, the software will display a message stating that the IMEI is considered valid; if not, it will be classified as invalid.

Sample Input and Output:

Example 1:

Example

Input:
Enter the 15-digit IMEI number: 490154203237518
Output:
The IMEI number is valid.

Example 2:

Output:

Output

Input:
Enter the 15-digit IMEI number: 490154203237519
Output:
The IMEI number is invalid.

Explanation of the Code:

Input Verification:

  • Initially, the software validates whether the provided input consists of precisely 15 characters.
  • Subsequently, it confirms the numeric nature of all characters within the input by utilizing the isNumeric functionality.

Luhn Algorithm:

For each digit in the IMEI number:

  • Every second digit from the right is doubled.
  • If the doubled value exceeds 9, it is reduced by 9.
  • The sum of all processed digits is calculated.
  • Finally, the sum is checked for divisibility by 10.
  • Key Features of the Program:

Some of the main characteristics of the software include:

Error Management:

  • It guarantees that any IMEI numbers that are not numeric or improperly formatted are identified as invalid.

Modular Design:

The functions such as isNumeric and isValidIMEI contribute to the modularity of the program, ensuring it remains easy to maintain.

User-Focused:

  • The application engages with the user via transparent messages and results.
  • Supplementary functionalities to enrich the software.

Batch Validation:

It enables users to verify numerous IMEI numbers in a single operation.

Example

char choice;
do 
{
    // Code to validate an IMEI
    std::cout << "Do you want to validate another IMEI? (y/n): ";
    std::cin >> choice;
} 
while (choice == 'y' || choice == 'Y');

It automatically removes spaces or dashes when a user inputs a formatted IMEI number (for example, 49-0154-203237-5-18).

IMEI Creator:

This tool provides the capability to produce a legitimate IMEI code for testing objectives.

Luhn Algorithm: Why It Works for IMEI?

The Luhn algorithm is effective in detecting common input mistakes such as swapping digits or single-digit errors. This method generates a distinct pattern by doubling every other digit and reducing large values, allowing validation through a simple modulus calculation.

Complexity Analysis:

Time Complexity:

The software handles each of the 15 numbers precisely one time.

O(n) where

n=15, effectively

O(1) because the input size is fixed.

Space Complexity:

The software utilizes a fixed amount of extra space, resulting in a space complexity of O(1).

Conclusion:

In summary, the Luhn algorithm is employed in this software to demonstrate methods for authenticating an IMEI code in C++. Due to its robustness, effectiveness, and flexibility, incorporating extra functionalities is simple. This utility ensures accurate verification by understanding the fundamental algorithm and format of IMEI codes, crucial for tasks involving mobile device management and security.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience