Skillrack Inlab Problem 10

Solutions for Skillrack CSE1001 Inlab 10

25-Oct-2016 to 12-Nov-2016

Total time : 20 mins

Challenges : 1

Question (Barcode)

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.”

Solution

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

Input

INPUT:
079400804501

Output

OUTPUT:
Validated
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s