RitinMalhotra

ISBN

Oct 29th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. /**
  2.  * This program inputs a number and checks whether the number entered is a valid ISBN or not.
  3.  */
  4. import java.util.Scanner;
  5. public class ISBN
  6. {
  7.     static int getSum(long num) //Function to get the required sum of all the digits.
  8.     {
  9.         int i, x, sum = 0;
  10.         String a = Long.toString(num);
  11.         for(i=0;i<a.length();i++)
  12.         {
  13.             x = Character.getNumericValue(a.charAt(i));
  14.             sum += (i+1) * x;
  15.         }
  16.        
  17.         return sum;
  18.     }
  19.    
  20.     public static void main(String[] args)
  21.     {
  22.         Scanner sc = new Scanner(System.in);
  23.         //Getting the ISBN.
  24.         System.out.println("Please enter a 10-digit ISBN.");
  25.         long num = sc.nextLong();
  26.         //Checking the length.
  27.         String a = Long.toString(num);
  28.         if(a.length() == 10)
  29.         {
  30.         int sum = getSum(num);
  31.         if(sum % 11 == 0)
  32.             System.out.println("The number entered is a valid ISBN!");
  33.         else
  34.             System.out.println("The number entered is not a valid ISBN.");
  35.         }
  36.         else
  37.             System.out.println("The number entered is not a valid ISBN. It must be exactly 10 digits long.");
  38.        
  39.         sc.close();
  40.     }
  41. }
Add Comment
Please, Sign In to add comment