Question

#include <stdio.h> int isValidCC(unsigned long long int CCNumber); int main() { unsigned long long int CCNumbers[]...

#include <stdio.h>

int isValidCC(unsigned long long int CCNumber);

int main()

{

unsigned long long int CCNumbers[] = {

4388576018410707ULL, // valid

4388576018402626ULL, // invalid

7388576018402686ULL, // invalid

438857601810707ULL, // invalid

4012888888881881ULL // valid

};

for (int i = 0; i < sizeof(CCNumbers) / sizeof(CCNumbers[0]); i++)

{

if (isValidCC(CCNumbers[i]))

{

printf("%llu is a valid Visa number.\n", CCNumbers[i]);

}

else

{

printf("%llu is not a valid Visa number.\n", CCNumbers[i]);

}

}

}

int isValidCC(unsigned long long int CCNumber)

{

// TO DO

}

Use one of the files above as a starting point. Only implement the isValidCC function. You do not need to change any other code. Other credit card numbers will be used to verify the function works correctly.

For this project, you will implement a function that validates Visa credit card numbers. The function will take an unsigned long long int and return an int (or bool). A file is provided for you to complete. You only need to implement the function. You must match the function prototype given. You will receive a 0 if your function implementation does not match the given prototype. Do not add any global variables.

A valid Visa credit card has the following properties:

  • starts with a 4
  • is 16 digits long
  • the Luhn algorithm results in a value that is a multiple of 10 (value % 10 = 0)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C program to validate visa card numbers

#include <stdio.h>

#include <stdlib.h>

// function declaration

int isValidCC(unsigned long long int CCNumber);

int main(void) {

               unsigned long long int CCNumbers[] = {

               4388576018410707ULL, // valid

               4388576018402626ULL, // invalid

               7388576018402686ULL, // invalid

               438857601810707ULL, // invalid

               4012888888881881ULL // valid

               };

               for (int i = 0; i < sizeof(CCNumbers) / sizeof(CCNumbers[0]); i++)

               {

               if (isValidCC(CCNumbers[i]))

               {

               printf("%llu is a valid Visa number.\n", CCNumbers[i]);

               }

               else

               {

               printf("%llu is not a valid Visa number.\n", CCNumbers[i]);

               }

               }

               return EXIT_SUCCESS;

}

// function to validate that the input CCNumber is a valid Visa Card number or not

int isValidCC(unsigned long long int CCNumber)

{

               int sum=0, digit;

               int count = 0;

               unsigned long long int CCNumCopy = CCNumber;

               // loop that analyzes all the digits of the card number

               while(CCNumCopy > 0)

               {

                              digit = CCNumCopy%10; // get the last digit

                              CCNumCopy = CCNumCopy/10; // get the number removing the last digit

                              if(count % 2 == 1) // check if odd digit, then double the digit

                              {              digit = 2*digit;

                                             if(digit > 9) // if the digit after doubling is greater than 9, subtract 9 from it to get the single digit

                                                            digit = digit - 9;

                              }

                              sum += digit; // add the digit to the sum

                              count++; //count the number of digits read thus far

               }

               // if first digit is 4 , number of digits is 16 and sum is a divisible by 10, then valid else invalid

               if((digit/2 == 4) && (count == 16) && (sum%10 == 0))

                              return 1;

               else

                              return 0;

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> int isValidCC(unsigned long long int CCNumber); int main() { unsigned long long int CCNumbers[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • 5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns...

    5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns igned int a, unsigned int b): // function prototype 5 int main(void) printf("%s". "Enter two positive integers: unsigned int x: I/ first integer unsigned int y: // second integer scanf("Su%u". &x, &y); "); 12 13 14 15 II Parameter b must be a positive integer 16 to prevent infinite recursion 7 unsigned int mystery Cuns igned int a, unsigned int b) 18 printf("The result...

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

  • C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial...

    C programming KAM5 Write a function unsigned long long int factorial(int num) to compute the factorial of a number. The factorial of a number is given by n! = 1*2* ... *n So 1! = 1, 2! = 1*2, 3! = 1*2*3 ... For example: Test Result printf("%llu\n", factorial(5)); 120 printf("%lu\n", factorial(20)); 2432902008176640000

  • How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary;...

    How do i finish this code: #include <stdio.h> int main() { long long decimal, tempDecimal, binary; int reminder, weight = 1; binary = 0.0; //Input decimal number from user printf("Enter any decimal number: "); scanf("%lld", &decimal); // Since we do not want change the value of decimal in the code // let us use tempDecimal to store the value of decimal tempDecimal = decimal; printf("\n\n"); // Let us use while loop first to implement printf("while loop part:\n"); /**************START FROM HERE...

  • Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main()...

    Having an issue with pointers and functions. #include <stdio.h> int f(int *a, int *b); int main() { int a = 2, b = 7; b = f(&b, &a); printf("a = %d,\n", a); printf("b = %d\n", b); return 0; } int f(int* a, int* b) {     (*a) = (*a) + 3;     (*b) = 2*(*a) - (*b)+5;     printf("a = %d b = %d\n", *a, *b);     return(*a)+(*b); } can someone explain to me why the output is a =...

  • Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main()...

    Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main() {    int i, j, n; int A[] = {2, 3, 5, 5, 5, 6, 8, 11, 11, 11};    n = sizeof(A) / sizeof(int);    for (i = 0; i < n - 1; i++){        if (A[i] != A[i + 1]) {            for (j = 1; j < n - 1; j++) {                A[j]...

  • #include <stdio.h> int main ( ) { int k; int sum=0; for (k=0; k<6; k++) {...

    #include <stdio.h> int main ( ) { int k; int sum=0; for (k=0; k<6; k++) { sum += k; } printf("%d\n", sum); return 0; } (a) [10 pts] Convert the C program to be an assembly code named as lab3_ex7_assembly.s. Please notice that your code must be executable in the Venus simulator. In the assembly code you write, you need to answer the following questions as well: (b) [5 pts] What are the registers in your code to represent the...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT