Program to check for ISBN | Java program to check if a given ISBN isvalid or not ; length must be 10 ; Computing weighted sum of first 9 digits ; Checking last digit . ; If last digit is ' X ' , add 10 to sum , else add its value ; Return true if weighted sum of digits is divisible by 11. ; Driver code
class GFG { static boolean isValidISBN ( String isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn . charAt ( i ) - '0' ; if ( 0 > digit 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn . charAt ( 9 ) ; if ( last != ' X ' && ( last < '0' last > '9' ) ) return false ; sum += ( ( last == ' X ' ) ? 10 : ( last - '0' ) ) ; return ( sum % 11 == 0 ) ; } public static void main ( String [ ] args ) { String isbn = "007462542X " ; if ( isValidISBN ( isbn ) ) System . out . print ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }