Question
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started