Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

COMSATS UNIVERSITY ISLAMABAD

Abbottabad Campus

Department of _______Computer Science_________

Assignment3FALL2022

Class: ____BCS 2C/2D____ Date: ___21/10/2022______


Subject: __Programming Fundamentals___ Instructor: _Nuhman Ul Haq__
Total Time Allowed: Max Marks: _____10________
Name:
Registration #

ANSWER THE FOLLOWING QUESTIONS


1. Credit card numbers follow certain patterns. A credit card number must have
between 13 and 16 digits. The numbermust start with the following:
a. 4 for Visa cards
b. 5 for MasterCard cards
c. 37 for American Express cards
d. 6 for Discover cards
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card
numbers. The algorithm is useful to determine whether a card number is
enteredcorrectlyor is scanned correctly by a scanner. Almost all credit card
numbers aregenerated following this validity check, commonly known as the
Luhn check orthe Mod 10 check. It can be described as follows. (For
illustration, consider thecard number 4388576018402626.)
I. Double every second digit from right to left. If doubling of a
digit results in atwo-digit number, add the two digits to get a
single digit number.

II. Now add all single-digit numbers from Step 1.


4 + 4+8+2+3+ 1+ 7+8=37
III. Add all digits in the odd places from right to left in the card
number.
6+ 6+0+8+ 0+7+8+ 3=38
IV. Sum the results from Step 2 and Step 3.
37+38=75
V. If the result from Step 4 is divisible by 10, the card number is
valid; otherwise, it is invalid. For example, the number
4388576018402626 is invalid, but thenumber
4388576018410707 is valid.
Write a program that prompts the user to enter a credit card number as a
string.
Display whether the number is valid. Design your program to use the
followingfunctions:

// Return true if the card number is valid


bool isValid(const string&cardNumber)
// Get the result from Step 2
int sumOfDoubleEvenPlace(const string&cardNumber)
// Return this number if it is a single digit, otherwise,
// return the sum of the two digits
int getDigit(int number)
// Return sum of odd-place digits in the card number
int sumOfOddPlace(const string&cardNumber)
// Return true if substr is the prefix for cardNumber
bool startsWith(const string&cardNumber, const string&substr)

PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool isValid(const char* cardNumber) {

if (strlen(cardNumber) < 13 || strlen(cardNumber) > 16) {


return false;
}

int sum1 = sumOfDoubleEvenPlace(cardNumber);

int sum2 = sumOfOddPlace(cardNumber);

return ((sum1 + sum2) % 10 == 0);


}
int sumOfDoubleEvenPlace(const char* cardNumber) {
int sum = 0;

for (int i = strlen(cardNumber) - 2; i >= 0; i -= 2) {


sum += getDigit(cardNumber[i] - '0');
}
return sum;
}

int getDigit(int number) {


if (number < 9) {
return number;
} else {

return (number / 10) + (number % 10);


}
}
int sumOfOddPlace(const char* cardNumber) {
int sum = 0;
for (int i = strlen(cardNumber) - 1; i >= 0; i -= 2) {
sum += (cardNumber[i] - '0');
}
return sum;
}

bool startsWith(const char* cardNumber, const char* substr) {


if (strlen(substr) > strlen(cardNumber)) {
return false;
}
return (strncmp(cardNumber, substr, strlen(substr)) == 0);
}

int main() {
char cardNumber[20];
printf("Enter a credit card number: ");
scanf("%s", cardNumber);

if (isValid(cardNumber)) {
printf("The credit card number is valid.\n");
} else {
printf("The credit card number is invalid.\n");
}

return 0;
}

You might also like