Question

C PROGRAM A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by...

C PROGRAM

A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by comparing the code’s last digit (called a check digit) to its own computation of the check digit from the first 11 digits as follows:

1. Calculate the sum of the digits in the odd-numbered positions (the first, third… eleventh digits) and multiply this sum by 3.

2. Calculate the sum of the digits in the even-numbered positions (the second, fourth… tenth digits) and add this to the previous result.

3. If the last digit of the result from step 2 is 0, then 0 is the check digit. Otherwise, subtract the last digit from 10 to calculate the check digit.

4. If the check digit matches the final digit of the 12-digit UPC, the UPC is assumed correct.

Write a program that prompts the user to enter the 12 digits of a barcode separated by pressing . The program should store the digits in an integer array, calculate the check digit, and compare it to the final barcode digit. If the digits match, output the barcode with the message “validated.” If not, output the barcode with the message “error in barcode.”

Also, output with labels the results from steps 1 and 2 of the check-digit calculations. Note that the “first” digit of the barcode will be stored in element 0 of the array.

Try your program on the following barcodes, three of which are valid. For the first barcode, the result from step 2 is 79 (0 + 9 + 0 + 8 + 4 + 0) * 3 + (7 + 4 + 0 + 0 + 5).

0 7 9 4 0 0 8 0 4 5 0 1

0 2 4 0 0 0 1 6 2 8 6 0

0 1 1 1 1 0 8 5 6 8 0 7

0 5 1 0 0 0 1 3 8 1 0 1

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Solution:

#include<stdio.h>

void main()

{

    int oddsum=0,evensum=0;

    int code[15],i,check_digit,last_digit,result;

    printf("Enter the barcode: ");

    for(i=0;i<12;i++)

        scanf("%d",&code[i]);

    

    for(i=0;i<12;i=i+2)

        oddsum=oddsum+code[i];

    

    for(i=1;i<10;i=i+2)

        evensum=evensum+code[i];

    oddsum=oddsum*3;

    result=oddsum+evensum;

    last_digit=result%10;

    if(last_digit!=0)

        check_digit=10-last_digit;

    else

        check_digit=last_digit;

    

    printf("\nSum of digits in odd positions multiplied by 3 is %d",oddsum);

    printf("\nSum of digits in odd positions multiplied by 3 added to Sum of digits in even positions is %d",result);

    

    

    if(check_digit==code[11])

        printf("\nvalidated\n");

    else

        printf("\nerror in barcode\n");

}

Output:

File Edit Selection View Go Debug Terminal Help barcode.c - Programs - Visual Studio Code -ox 2.2 D 0 - EXPLORER e C barcode.File Edit Selection View Go Debug Terminal Help barcode.c - Programs - Visual Studio Code -ox 19 D 0 EXPLORER e C barcode.c X

Add a comment
Answer #2
#include<stdio.h>
void main()
{
    int oddsum=0,evensum=0;
    int code[15],i,check_digit,last_digit,result;
    printf("Enter the barcode: ");
    for(i=0;i<12;i++)
        scanf("%d",&code[i]);
    
    for(i=0;i<12;i=i+2)
        oddsum=oddsum+code[i];
    
    for(i=1;i<10;i=i+2)
        evensum=evensum+code[i];
    oddsum=oddsum*3;
    result=oddsum+evensum;
    last_digit=result%10;
    if(last_digit!=0)
        check_digit=10-last_digit;
    else
        check_digit=last_digit;
    
    printf("\nSum of digits in odd positions multiplied by 3 is %d",oddsum);
    printf("\nSum of digits in odd positions multiplied by 3 added to Sum of digits in even positions is %d",result);
    
    
    if(check_digit==code[11])
        printf("\nvalidated\n");
    else
        printf("\nerror in barcode\n");
}


answered by: Tayyab Shahzad
Add a comment
Know the answer?
Add Answer to:
C PROGRAM A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • Introduction: Ten digit ISBN numbers are created so that the first nine digits are information digits...

    Introduction: Ten digit ISBN numbers are created so that the first nine digits are information digits and the last digit is a check digit. T his last number helps people notice and correct mistakes that might be made in recording the information digits. The same is true for thirteen digit ISBN numbers. Here is a ten digit ISBN number: 0-13-149498-8. The digit 0 indicates the book is written for English-speaking people. The number 13 and the number 149498 identify the...

  • Write a C program which calculates how many digits a number has and prints each digit...

    Write a C program which calculates how many digits a number has and prints each digit in reserve order with space in between(no space after the last digit). The number > 0 and has no more than 5 digits. (optional] It is better to write two functions. One is designed for printing number in reverse order and the other is for counting the number of digits. Given that the value of number (which is an int variable) is 12345, the...

  • Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy...

    Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy the first nine digits are valid. Before 2007, all ISBN numbers were composed like this, such as: e-20-5e8005-7 or 1-234-56789-X The first nine digits are assigned by a book's publisher and the last digit is calculated by "weighted sum (described below). The X stands for the checksum value of 10, in order to represent ten as a single digit. You must write a program...

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • I need it in c++ The U.S. Banking System The code assigned to a bank is...

    I need it in c++ The U.S. Banking System The code assigned to a bank is an eight digit number plus a ninth check digit. To check for validity, the first eight digits are assigned weights (left to right) of 7, 3, and 9 repetitively. Each digit is multiplied by its weight and the products summed. The resulting sum (mod 10) should equal the check digit. The assigning of weights insures that all single digit errors and most transposition errors...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an...

    C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an array of numbers and produce the following output: 1. Your first and last name 2. Course Number 3. C Project Number 4. All the numbers in the array 5. The sum of all the numbers in the array 6. The count of all the numbers in the array 7. The average of all the numbers in the array Double-space after lines 3, 4, 5,...

  • Please I need help with this c++ code. please show all steps , write comments and...

    Please I need help with this c++ code. please show all steps , write comments and show sample runs. Thank you. 1. The Federal Bureau of Investigation (FBI) has recently changed its Universal Control Numbers (UCN) for identifying individuals who are in the FBI's fingerprint database to an eight-digit base 27 value with a ninth check digit. The digits used are: 0123456789ACDE FHJKLMNPRTVWX Some letters are not used because of possible confusion with other digits: B->8, G->C, I- >1, 0->0,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT