\$\begingroup\$Loops have and some basic functions like checking for a positive integer. I did think about looping through variables which I saw required vectors or arrays but haven't got to that yet so don't really understand.\$\endgroup\$
– Dan SuttonJun 15 at 13:13

1 Answer
1

It might be better to treat the credit card number as a string. In the C programming language a string is a null terminated array of type char or character. This would remove all the division in the program to get each character. It would also allow the program to detect if any non-numeric characters were entered.

To get the actual numeric value of a character you would subtract '0' from the numeric character. For a single character this would always give you a value between zero and nine.

Variable Names
Having variables i1 through i16 is a very good indication that i should be an array, this is also true of t.

Having single character variable names is generally frowned upon except for loop control values. A single character really doesn't tell anyone reading or modifying the code what the variable really is or does. It isn't really clear in the program what i or t represents. While number is longer, it might be better if it was credit_card_number.

Basic Principles When Writing Code
One of the earliest principles all programmers learn is Don't Repeat Yourself, usually shortened to DRY code. Basically anytime you have code that repeats it would be better to put it into a loop or a function where the code is reused.

There is a second form of the if statement that could also make the code shorter, it is generally covered in the later part of any C programming course

tN = (tN > 9)? tN - 9 : tN;

This single statement is equivalent to the function above.

A second example of repeating code is the division to reduce each digit in the credit card number to a single number, this could also be put into a loop. The divisor could be reduced in each iteration of the loop if the algorithm sticks with using numbers.

A second principle that should be taught early but is part of more complex programming is the Single Responsibility Principle which states that every module, class, or function should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated within the function, module or class. This reduces the size of functions which allows functions to be more readable, maintainable and easier to debug. This would mean breaking the main() function into two or three sub functions to reduce the complexity of main.

Use Vertical Spacing to Make the Code More Readable
The code in the question has the if (tN > 9) on only two lines, it might be more readable if it was 4 lines as shown above.

\$\begingroup\$There are (or at least there was, not sure if it's still the case nowadays) cards with 19-digit PANs.\$\endgroup\$
– jcaronJun 15 at 22:27

\$\begingroup\$@jcaron Are/were they in Europe or Asia?\$\endgroup\$
– pacmaninbwJun 15 at 22:35

1

\$\begingroup\$@pacmaninbw According to Wikipedia payment card numbers can range from 10 (formerly 8) to 19 digits, though in practice they actually range from 12 to 19. Examples include Maestro, China UnionPay, Diners Club, Discover, and JCB.\$\endgroup\$
– jcaronJun 16 at 15:35

1

\$\begingroup\$A string is absolutely the correct approach - after all, long (or even unsigned long) isn't necessarily large enough to represent decimals of the size required (9 digits are okay, but more than that is platform-dependent).\$\endgroup\$
– Toby SpeightJun 17 at 15:12