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

In beginner programming - C code: If you buy something online and mistype your c

ID: 673909 • Letter: I

Question

In beginner programming - C code:

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>

int main(){
   printf("How many digits on card?"" );
   int d;
   scanf("%d", &d);
   printf("Enter card number");
   long int card;
   scanf("%ld", &card);
   long int cpy = card;
   int sum = 0;
   while(cpy > 0){
       sum += cpy%10;
       cpy/=100;
   }
   cpy = card/10;
   while(cpy > 0){
       int x = cpy%10;
       x = x*2;
       while(x >= 10){
           int tmp = x;
           int res = 0;
           while(tmp > 0){
               res += tmp%10;
               tmp/=10;
           }
           x = res;
       }
       sum += x;
       cpy /= 100;
   }

   if(sum%10 == 0){
   printf("Valid Card");
   }
   else
   printf("Not a valid card");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote