Sunday, January 8, 2017

// // Leave a Comment

Exercise 3.9 — Business: Check ISBN-10

Program that prompts the user to enter the first 9 digits of an ISBN number and displays the 10-digit ISBN (including leading zeros).

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 */

import java.util.Scanner;

public class Ex03_09 {
    public static void main(String[] args) {
        int isbnFirstNineDigits, d1, d2, d3, d4, d5, d6, d7, d8, d9, isbnLastDigit;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter the first 9 digits of an ISBN as integer: ");
 isbnFirstNineDigits = input.nextInt();

        d9 = isbnFirstNineDigits % 10;
        d8 = (isbnFirstNineDigits / 10) % 10;
        d7 = (isbnFirstNineDigits / 100) % 10;
        d6 = (isbnFirstNineDigits / 1000) % 10;
        d5 = (isbnFirstNineDigits / 10000) % 10;
        d4 = (isbnFirstNineDigits / 100000) % 10;
        d3 = (isbnFirstNineDigits / 1000000) % 10;
        d2 = (isbnFirstNineDigits / 10000000) % 10;
        d1 = (isbnFirstNineDigits / 100000000) % 10;

        isbnLastDigit = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 
                + d7 * 7 + d8 * 8 + d9 * 9) % 11;

        if (isbnLastDigit == 10) {
            System.out.println("The ISBN-10 number is " + d1 + d2 + d3 + d4 + d5
                + d6 + d7 + d8 + d9 + "X");
        } else
        System.out.println("\nThe ISBN-10 number is " + d1 + d2 + d3 + d4 + d5
                + d6 + d7 + d8 + d9 + isbnLastDigit);
    }
}
Liang Introduction to Java Programming exercise 3.9 solution
Program sample run

Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.