Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please only use the basic knowedge such as<stdio.h>,<math.h>,and loop or arrays

ID: 672928 • Letter: P

Question

please only use the basic knowedge such as<stdio.h>,<math.h>,and loop or arrays in c language.

If you buy something online and mistype your credit card number, the website can tell you instantly that it's not a valid card number. How? The Luhn algorithm, which you'll be implementing for this assignment.

The Luhn algorithm computes a "checksum," a simple calculation based on the digits of your card. For a valid card, this checksum value is always 0.

The Luhn algorithm works as follows:

Starting with the last digit (which is special for creating card numbers, but not for checking an existing number), take the sum of every other digit (every other meaning skip one in between).

Then, starting with the second to last digit, take every other digit and double it. Add each digit of the doubled value to the checksum.

If the card is valid, the final checksum will end with 0.

Here's example of a 4 digit card number

so the checksum for this card is 11, which does not end in 0, which means it's invalid.

Write a program to ask the user for the number of digits in the card. If it's odd, make it even by adding a leading 0. For example, a 3 digit card 123 becomes the 4 digit card 0123.

Then input the card number, without making the user worry about white space. Compute the Luhn check, then tell the user if the card is valid or not. Your program should look something like the following when running:

Explanation / Answer

#include <stdio.h>
#include <string.h>
int main()
{
char card[20], finalCard[20] = "0";
int n, size, sum1 = 0, sum2 = 0, doubled, i, numOfDigits;
printf("How many digits is the card: "); /*Reading the number of digits in the card.*/
scanf("%i",&numOfDigits);
printf("Enter the card number: ");
scanf("%s",card);               /*Reading the card number.*/
while(strlen(card) != numOfDigits)       /*If the number of digits in the card is not as specified re-read.*/
{
printf("Number of digits doesn't match your specified number: ");
scanf("%s",card);
}
if(numOfDigits % 2 != 0)   /*If odd number of digits, append 0 to the left.*/
strcat(finalCard,card);
else
strcpy(finalCard,card);
size = strlen(finalCard);
for(i = size-1; i >= 0; i -= 2)   /*Starting from last digit every alternate digit is added.*/
sum1 += finalCard[i] - '0';
for(i = size-2; i >= 0; i -= 2) /*Starting from penultimate digit every alternate digit is doubled and the digits in that number is added.*/
{
doubled = 2 * (finalCard[i] - '0');
sum2 += doubled %10 + doubled / 10;
}
if((sum1+sum2)%10 == 0)           /*Finally when adding both these numbers, and checking whether the last digit is ZERO.*/
printf("The card number is valid. "); /*If YES, display VALID Card.*/
else
printf("Sorry, your card is invalid. "); /*If NOT, display INVALID Card. */
}