Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C PROGRAM! Hello! Can you help me to find the problem here? It prints Its not valid for every card number. This is the problem

C PROGRAM! Hello! Can you help me to find the problem here? It prints "Its not valid" for every card number. This is the problem of Hans Luhn of IBM proposed an algorithm for validating credit card numbers.

#include #include #include bool isValid (char cardNumber [] ){ int l=strlen(cardNumber); if(l>=13 && l<=16) return true; else return false; }

int getDigit (int number){ int val, total=0; while(number>0){ val=number%10; total+=val; number=number/10; } if(total>10){ getDigit(total); } return total; } int sumOfOddPlace(char cardNumber [] ){ int l=(unsigned)strlen(cardNumber); int sum_odd=0; for(int i=l-1; i>=0;i=i-2){ sum_odd+=(cardNumber[i]-'0'); } return sum_odd; }

int sumOfDoubleEvenPlace (char cardNumber [] ){ int l=(unsigned)strlen(cardNumber); int sum_even=0,val; for(int i=l-2; i>=0; i=i-2){ val=(cardNumber[i]-'0')*2; if(val>=10){ sum_even+=getDigit(val); } else sum_even+=val; } return sum_even; }

int main() { char cardnum[16]; printf("Enter card number: "); scanf("%lld", &cardnum);

int sumOdd,sumEven,total; bool status; status=isValid(cardnum); sumOdd=sumOfOddPlace(cardnum); sumEven=sumOfDoubleEvenPlace(cardnum); total=sumOdd+sumEven; if(status && total%10 == 0){ printf("It is valid"); } else { printf("It is not valid"); }}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 2 Lnai 9285

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Joao Gama ,Alipio Jorge ,Carlos Soares

1st Edition

3319235249, 978-3319235240

More Books

Students also viewed these Databases questions

Question

understand the key issues concerning international assignments

Answered: 1 week ago