Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. /*
  2. Program for
  3. Written by: Alec
  4. Wirrten on: Nov 2018
  5. Written in: C
  6. Compilied with: gcc
  7. Version 7.2.1
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. int main(void){
  13. //Defines variables
  14. int n1 = 0; //first 6 numbers
  15. int n2 = 0; //2nd 6 numbers
  16. int array1[6]; //number 1 array that contains 1st number set
  17. int array2[6]; //second array containg numbers from n2
  18. int count1 = 6; //sets array size later
  19. int count2 = 6; //same
  20.  
  21. //Ask user to input data an reads it
  22. printf("Enter the first 6 digits of the barcode: \n");
  23. scanf("%i", &n1);
  24. printf("Enter the first 6 digits of the barcode: \n");
  25. scanf("%i", &n2);
  26.  
  27. //Splits number1 into individual digits
  28.    count1 = 0;
  29.    while (n1 > 0){
  30.        array1[count1] = n1 % 10;
  31.        n1 /= 10;
  32.        count1++;
  33.    }
  34.  
  35.    count2 = 0;
  36.    while (n2 > 0){
  37.        array2[count2] = n2 % 10;
  38.        n2 /= 10;
  39.        count2++;
  40.    }
  41. //Steps 1-3n
  42. int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
  43. int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
  44. int sumd = 3*sumo; //multiplies odds
  45. int sum  = sume+sumd; //adds above and evens
  46. int chec = sum%10;
  47. int check = 10-chec;
  48.  
  49. //Step 4
  50. if(chec == 0){ //Checks if last digit is 0
  51.     if(chec == array2[0]){
  52.         printf("The UPC is legal\n");
  53.     }
  54.     else{
  55.         printf("The UPC is illegal\n");
  56.     }
  57. }
  58. if(check == array2[0]){ //Sees if check digit is legal
  59.     printf("The UPC is legal\n");
  60. }
  61. else{
  62.     printf("The UPC is illegal\n");
  63. }
  64. printf("Digit 1 of array 1 is %i\n", array1[5]);
  65. printf("Digit 1 of array 2 is %i\n", array2[5]);
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement