c - Is this correct or is there a better way? -


i'm trying write c program reads barcode(that 10 digits long) stdin, adds first 9 digits of barcode, , if second digit of sum of 9 digits equal 10th digit in barcode writes barcode stdout.

here have far, can 1 please me or tell me wrong i've got or add. possible put whole barcode character array , sum first 9 digits , compare value of bar[9]? how this? thanks

#include <stdio.h>  int main(void){   int sum = 0;   char bar[10];   int i;   for(i = 0; < 9; i++){     scanf("%d", &bar[i]);   }    for(i = 0; < 10; i++){      sum += bar[i];   }    if(sum[1] == bar[10]){       return 1;   }   return 0; } 

corrections

  1. change char bar[10] int bar[10], because handling intergers
  2. array indexes begin 0 , end @ sizeofarray-1
  3. primitives int, float variables cannot referred using indexes have done in sum[1] == bar[10]. here sum int type bar array of integers.
  4. logic test last 2 digit , tenth digit of barcode

    int seconddigit = sum%100; // 189 resturns 89 seconddigit = seconddigit/10 // 89 return 8  if(seconddigit == bar[9]) // see sizeofarray 10 - 1 = 9 last array element.  {      // wanted display barcode. } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -