C++ program to check for ISBN

C++Server Side ProgrammingProgramming

Given with the sequence and the task is to identify whether the given sequence is sa ISBN number or not.

What is an ISBN number

ISBN stands for International Standard Book Number is a 10 digit number till December 2006 and now it is revised to 13 digit number from 1 January 2007. Given below is the implementation of 10 digits ISBN.

ISBN digit has a pattern in it as −

  • Starting 9 digits of a number represents Title, Publisher and Group of the book. The value of first 9 digit can range from 0 - 9
  • Last 1 digit checks whether the ISBN is correct or not. Last digit can take the value as 10 and symbol ‘X’ is used to represent this number

How to check whether a given 10 digit number is an ISBN number or not?

  • Start from the first digit of a sequence and multiply it with 10 as ISBN is a 10 digit number
  • Keep moving to the next digit by reducing the value of multiplier by 1
  • Add all the multiplied results
  • Divide the result with 11
  • Apply condition to check for ISBN number
    • If result is divisible by 11 which means leaves no remainder or 0 as remainder than number is an ISBN number
    • If result isn’t divisible by 11 which means leaves some remainder or not 0 as remainder than number isn’t an ISBN number

Example

Input-: 002442142X
Output-: Not ISBN
Input-: 007462542X
Output-: it’s ISBN

Algorithm

Start
Step 1-> declare function to check for ISBN
   bool isvalid(string &isbn_number)
      set int n = isbn_number.length()
         IF (n != 10)
            return false
         End
         Set int sum = 0
         Loop For int i = 0 and i < 9 and i++
            Set int value = isbn_number[i] - '0'
            If (0 > value || 9 < value)
               return false
            End
            Set sum += (value * (10 - i))
         End
         Set char last_val = isbn_number[9]
         IF (last_val != 'X' && (last_val < '0' || last_val > '9'))
            return false
         End
         Set sum += ((last_val == 'X') ? 10 : (last_val - '0'))
         return (sum % 11 == 0)
step 2-> In main()
   set string isbn_number = "002442142X"
   IF (isvalid(isbn_number))
      Print " is Valid"
   End
   Else
      Print " is invalid
   End
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//check whether its a valid ISBN or not
bool isvalid(string &isbn_number) {
   int n = isbn_number.length();
   if (n != 10)
      return false;
   int sum = 0;
   for (int i = 0; i < 9; i++) {
      int value = isbn_number[i] - '0';
      if (0 > value || 9 < value)
         return false;
      sum += (value * (10 - i));
   }
   char last_val = isbn_number[9];
   if (last_val != 'X' && (last_val < '0' || last_val > '9'))
      return false;
   sum += ((last_val == 'X') ? 10 : (last_val - '0'));
   return (sum % 11 == 0);
}
int main() {
   string isbn_number = "002442142X";
   if (isvalid(isbn_number))
      cout<<isbn_number<<" is Valid";
   else
      cout<<isbn_number<<" is invalid";
   return 0;
}

Output

IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT

002442142X is invalid
raja
Updated on 18-Oct-2019 09:10:26

Advertisements