/* Copyright 2018 Ali Şentaş Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "stdio.h" int get_digit(long long id, int digit); int validate(long long id); int main(){ long long id; // it's long long because it's huge // get input printf("Enter ID > "); scanf("%ld", &id); printf("ID to be tested: %lld\n", id); // we won't do any input checking here because it's out of scope of the article // we assume user inputs an eleven digit number if(validate(id)) printf("%lld is a valid Turkish ID number.\n"); else printf("%lld is not a valid Turkish ID number.\n"); return EXIT_SUCCESS; } // this function will return ID digit in given index // to follow the blog article, index is 1 based // if ID is 10000000146 then: // index = 1 => 1 // index = 2 => 0 // index = 10 => 4 // you get the idea int get_digit(long long id, int index){ // below loop will increment index and divide the id accordingly // for example if index is 10 then it's incremented by one and id will be // divided by ten only once resulting id will be 10-digits and last digit // will be returned after that operation while(index < 11){ id /= 10; ++index; } return id % 10; } // this function will return true if given id is valid according // to the algorithm int validate(long long id){ int A, B; // validate first rule A = get_digit(id, 1) + get_digit(id, 3) + get_digit(id, 5) + get_digit(id, 7) + get_digit(id, 9); B = get_digit(id, 2) + get_digit(id, 4) + get_digit(id, 6) + get_digit(id, 8); if(get_digit(id, 10) != (7*A - B) % 10) return 0; // validate second rule A = get_digit(id, 1) + get_digit(id, 2) + get_digit(id, 3) + get_digit(id, 4) + get_digit(id, 5) + get_digit(id, 6) + get_digit(id, 7) + get_digit(id, 8) + get_digit(id, 9) + get_digit(id, 10); if(get_digit(id, 11) != A % 10) return 0; return 1; }