Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program for
- Written by: Alec
- Wirrten on: Nov 2018
- Written in: C
- Compilied with: gcc
- Version 7.2.1
- */
- #include <stdio.h>
- int main(void){
- //Defines variables
- int n1 = 0; //first 6 numbers
- int n2 = 0; //2nd 6 numbers
- int array1[6]; //number 1 array that contains 1st number set
- int array2[6]; //second array containg numbers from n2
- int count1 = 6; //sets array size later
- int count2 = 6; //same
- //Ask user to input data an reads it
- printf("Enter the first 6 digits of the barcode: \n");
- scanf("%i", &n1);
- printf("Enter the first 6 digits of the barcode: \n");
- scanf("%i", &n2);
- //Splits number1 into individual digits
- count1 = 0;
- while (n1 > 0){
- array1[count1] = n1 % 10;
- n1 /= 10;
- count1++;
- }
- count2 = 0;
- while (n2 > 0){
- array2[count2] = n2 % 10;
- n2 /= 10;
- count2++;
- }
- //Steps 1-3n
- int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
- int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
- int sumd = 3*sumo; //multiplies odds
- int sum = sume+sumd; //adds above and evens
- int chec = sum%10;
- int check = 10-chec;
- //Step 4
- if(chec == 0){ //Checks if last digit is 0
- if(chec == array2[0]){
- printf("The UPC is legal\n");
- }
- else{
- printf("The UPC is illegal\n");
- }
- }
- if(check == array2[0]){ //Sees if check digit is legal
- printf("The UPC is legal\n");
- }
- else{
- printf("The UPC is illegal\n");
- }
- printf("Digit 1 of array 1 is %i\n", array1[5]);
- printf("Digit 1 of array 2 is %i\n", array2[5]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement