Inlab 10

INLAB 10
A barcode scanner for Universal Product Codes (UPCs) verifies the 12-digit code scanned by comparing the code’s last digit (called as 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, to get dig_Sum.
  3. If the last digit of the result from step 2 is 0, then the check digit is 0. Otherwise, check digit is obtained by subtracting last digit of dig_Sum from 10 (10 - last digit of dig_Sum).
Develop an algorithm and write a C program to read the barcode, if the number of digits is not 12 then print “Cannot be processed” and terminate program. Otherwise calculate the check digit, and compare it to the last barcode digit.
If the digits match, output the barcode with the message “Validated”. If not, output the barcode with the message “Error in barcode.” For example the barcode code 079400804501, the result from step 2 is 79 (0 + 9+ 0 + 8 + 4 + 0) * 3 + (7 + 4+ 0 + 0 + 5). Since the last digit is 9, check digit is 10 -9 = 1 and this matches the last digit of USD therefore print ‘Validated’.
Input format
Barcode
Output
If number of digits is not 12, then print “Cannot be processed”
If check digit matches the last digit of USD then print the message “Validated”
If not, output the barcode with the message “Error in barcode.”

 Program:
#include<stdio.h>
#include<string.h>
int main() 
{
    char UGC[13];
    scanf("%s",&UGC);
    if(strlen(UGC) ==12)
    {
    int sum = 0;
    for(int i = 0;i<11;i+=2)
        sum += (UGC[i] - '0');
    sum *= 3;
    for(int i = 1;i<11;i+=2)
        sum += (UGC[i] - '0');
    if ((UGC[11] == '0') && ((sum%10)==0))
        printf("Validated");
    else if ((UGC[11]-'0') == (10-(sum%10)))
        printf("Validated");
    else
        printf("Error in barcode");
   }
   else
      printf("Cannot be processed");
   return 0;
}

No comments:

Post a Comment

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...