Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Check digit generator, for comp. sci. AS level example
- * Ashley Dixon, 2018 */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int main ( int argc, char ** argv ) {
- const int weights[] = { 1, 3 };
- unsigned int len = 0, sum = 0;
- char * in = NULL;
- if ( argc < 2 ) {
- fprintf ( stderr, "error: no arguments provided\n" );
- return EXIT_FAILURE;
- }
- in = argv[1];
- len = strlen ( in );
- for ( unsigned int i = 0; i < len; i++ ) {
- if ( !isdigit ( in[i] ) ) {
- fprintf ( stderr, "error: %c is not a valid digit (0-9)\n", in[i] );
- return EXIT_FAILURE;
- }
- sum += weights[ i & 1 ] * ( in[i] - '0' );
- }
- printf ( "digit: %d\n", 10 - ( sum % 10 ) );
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment