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
- change char bar[10] int bar[10], because handling intergers
- array indexes begin 0 , end @
sizeofarray-1
- primitives
int
,float
variables cannot referred using indexes have done insum[1] == bar[10]
. here sum int type bar array of integers. 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
Post a Comment