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

This preview shows page 1 - 4 out of 8 pages.

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
Image of page 1
Answer : Implementation: • Take a 12 digit code from user. Use integer array to store this 12 digit code. • The array index start from 0 and goes to 11. • Use a for() loop to take input from user. /*for() loop to initilize value from 0-11*/ for(i=0;i<12;i++) { scanf("%d",&arr[i]); /*read input*/ } • Use another for() loop to calculate the sum of digits in the odd- numbered positions and even-numbered positions. • Inside the for() a j variable is initialized with a value of 1. This variable is incremented by a unit value for each iteration performed by the loop. • This j variable is used to find out the even and odd positions. This is done by using an if() statement. If j is divisible by 2 then it calculates the sum of even number else the same operation is done for odd numbers. for(i=0;i<11;i++) { j = j+1; /* *check the value of 'j' is divisible by 2 or not *if() statement will executes if it is not divisible by 2, *else else() part will execute
Image of page 2
*/ if(j%2!=0) { /*sum of digits in the odd-numbered positions*/ odd_sum = odd_sum + arr[i]; } else { /*sum of digits in the even-numbered positions*/ even_sum = even_sum + arr[i]; } } • Multiply the sum of odd-numbered position by 3. odd_sum = (odd_sum*3);/*multiply by 3 to odd_sum*/ • Calculate the sum of even-numbered and odd-numbered positions. sum = odd_sum + even_sum; • Find the last digit of sum, by using modulus operator(%). last_digit = sum%10; /*to find the last digit*/ • If the last digit of sum is equals to 0 then print “validated†± , else subtract the last digit from 10 and compare with the final digit of the 12 digit. If both are equal then print “validate†± else print “error in bar code†± .
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 8 pages?

  • Summer '16
  • boer
  • Elementary arithmetic, Even and odd functions, Parity, Evenness of zero, sum of the digits

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes