A barcode scanner for Universal Product Codes (UPC) verifies the 12-digit code t
ID: 3641136 • Letter: A
Question
A barcode scanner for Universal Product Codes (UPC) verifies the 12-digit codethat was scanned by comparing the code's last digit (called a check digit) to a computed
value 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,
the third, ..., eleventh digit) and multiply this sum by 3.
2. Calculate the sum of the digits in the even-numbered positions (the second,
the fourth, ... tenth digit) 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, then the UPC
is assumed to be correct.
Write a program that reads several 12-digit barcodes and prints if each of
these barcodes is valid ("validated") or not ("error in barcode").
The first line of the input contains the number of barcodes N. The next N lines
contain the 12-digit barcodes.
Remarks: Please replicate the exact text of the prompts and the output. Even
though the text must be the same, the numbers may be different depending on the
user input.
============= START OF SAMPLE RUN =======================
---------------- START OF INPUT -------------------------
4
079400804501
024000162860
011110856807
051000138101
----------------- END OF INPUT --------------------------
---------------- START OF OUTPUT ------------------------
validated
error in barcode
validated
validated
----------------- END OF OUTPUT -------------------------
============= END OF SAMPLE RUN =======================
Explanation / Answer
please rate - thanks
#include <stdio.h>
int main()
{int n, i,j,digit[12],sum,check;
char upc[20][12],enter;
printf("============= START OF SAMPLE RUN ======================= ");
printf("---------------- START OF INPUT ------------------------- ");
scanf("%d",&n);
scanf("%c",&enter);
for(i=0;i<n;i++)
{for(j=0;j<12;j++)
scanf("%c",&upc[i][j]);
scanf("%c",&enter);
}
printf("----------------- END OF INPUT -------------------------- ");
printf("---------------- START OF OUTPUT ------------------------ ");
for(i=0;i<n;i++)
{for(j=0;j<12;j++)
{ digit[j]=(upc[i][j]-'0');
}
sum=0;
for(j=0;j<11;j+=2)
{sum+=digit[j];
}
sum*=3;
for(j=1;j<11;j+=2)
sum+=digit[j];
if(sum%10==0)
check=0;
else
check=10-(sum%10);
if(check==digit[11])
printf("validated ");
else
printf("error in barcode ");
}
printf("----------------- END OF OUTPUT ------------------------- ");
printf("============= END OF SAMPLE RUN ======================= ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.