+ 1

Whats the problem in this code? last 2 test case didn't match

https://code.sololearn.com/cxtu19OQcJI4/?ref=app Below is the problem : You need to verify if the given credit card number is valid. For that you need to use the Luhn test. Here is the Luhn formula: 1. Reverse the number. 2. Multiple every second digit by 2. 3. Subtract 9 from all numbers higher than 9. 4. Add all the digits together. 5. Modulo 10 of that sum should be equal to 0. Task: Given a credit card number, validate that it is valid using the Luhn test. Also, all valid cards must have exactly 16 digits. Input Format: A string containing the credit card number you need to verify. Output Format: A string: 'valid' in case the input is a valid credit card number (passes the Luhn test and is 16 digits long), or 'not valid', if it's not. Sample Input: 4091131560563988 Sample Output: valid Update : Solved by just adding if (n == 16) condition. and nested the whole process into it. else will be printed "not valid"

9th Jun 2020, 1:52 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
11 Antworten
+ 2
Yes, thanks, I'm reading...
9th Jun 2020, 2:13 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
You could copy...
9th Jun 2020, 2:07 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
I'm a bit confused of the middle-part, the second loop. Can't test it, but I would at least shorten it a bit: for (i = 0; i < n; i + 2) { num[i] = (str[i] * 2); if ( num[i] > 9){ num[i]-=9; }
9th Jun 2020, 2:19 PM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Thanks All 😊 Just solve it by just doing if (n != 16) the condition going execute not valid. just a little fix
9th Jun 2020, 6:50 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
0
This is a Pro-code coach, so you'll have better chances for help, if you would at least post the description.
9th Jun 2020, 2:03 PM
Sandra Meyer
Sandra Meyer - avatar
0
Sandra Meyer I've to manually write the problem?
9th Jun 2020, 2:05 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
0
Sandra Meyer Now Is it okay?
9th Jun 2020, 2:09 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
0
Martin Taylor oh, i just need to filter it with the 0-9 condition? like if (str[i] >= '0' && str[i] <= '9')
9th Jun 2020, 2:49 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
0
Martin Taylor I used gets because fgets gives me 1 length more
9th Jun 2020, 3:27 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
0
Martin Taylor Still (after filtering by isdigit and 0-9 also) Number 6, 7 test case failed like earlier. * 6 & 7 case are locked. so, I am unable to find the bug
9th Jun 2020, 6:41 PM
Jubaer Ahmad
Jubaer Ahmad - avatar
0
Hey Jubaer Ahmad, I Am Posting My Code Here Based On Luhn’s Algorithm To Validate A Credit Card: https://code.sololearn.com/cIo1IRS8tYRM/?ref=app My Code Passed All The Test Cases So With The Help Of This Code You May Find Some Help. ✌️✌️
11th Jun 2020, 5:52 AM
Rahul Saxena
Rahul Saxena - avatar