Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This program inputs a number and checks whether the number entered is a valid ISBN or not.
- */
- import java.util.Scanner;
- public class ISBN
- {
- static int getSum(long num) //Function to get the required sum of all the digits.
- {
- int i, x, sum = 0;
- String a = Long.toString(num);
- for(i=0;i<a.length();i++)
- {
- x = Character.getNumericValue(a.charAt(i));
- sum += (i+1) * x;
- }
- return sum;
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- //Getting the ISBN.
- System.out.println("Please enter a 10-digit ISBN.");
- long num = sc.nextLong();
- //Checking the length.
- String a = Long.toString(num);
- if(a.length() == 10)
- {
- int sum = getSum(num);
- if(sum % 11 == 0)
- System.out.println("The number entered is a valid ISBN!");
- else
- System.out.println("The number entered is not a valid ISBN.");
- }
- else
- System.out.println("The number entered is not a valid ISBN. It must be exactly 10 digits long.");
- sc.close();
- }
- }
Add Comment
Please, Sign In to add comment