# What do you think about my code here?

``````#include <cs50.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
long long credit_num;
credit_num = get_long_long("credit number: ");

// for get the length of the credit_num:
int length = floor(log10(llabs(credit_num))) + 1;

int sum1 = 0;
int sum2 = 0;

long long pe_of_credit = 10;
if (length == 13 || length == 15 || length == 16)
{
// a loop for get lasts and multply them by 2:
for (int i = 0; i < length; i++, pe_of_credit *= 100)
{
// for get last (e.i "3"4"5"6"6"7):
int last = ((credit_num - credit_num % pe_of_credit) / pe_of_credit) % 10;
// multply it by 2:
int last_mul_2 = last * 2;

// if the result number is over than one digit then we want to break it into 2 numbers:
// for calculate the length of the result number:
int length_of_last = floor(log10(labs(last_mul_2))) + 1;
if (length_of_last >= 2)
{
int pe_of_last = 1;
int last1 = 0;
for (int j = 0; j < length_of_last; j++, pe_of_last *= 10)
{
int last_of_last = ((last_mul_2 - last_mul_2 % pe_of_last) / pe_of_last) % 10;
last1 += last_of_last;
}
last_mul_2 = last1;
}
// and if the reesult number is just 1 digit then it wel ignor the prevous if, and just adding the result to the sum:
sum1 += last_mul_2;
}
// for get other last (e.i 3"4"5"6"6"7"):
long long pe_of_credit_other_num = 1;
for (int i = 0; i < length; i++, pe_of_credit_other_num *= 100)
{
int last = ((credit_num - credit_num % pe_of_credit_other_num) / pe_of_credit_other_num) % 10;
sum2 += last;
}
// adding the sums of the previos calcluts:
int sum = sum1 + sum2;
// getint the last of the sum:
int last_of_sum = sum % 10;

if (last_of_sum == 0)
{
long long pe_visa = pow(10, length - 1);
long long pe_master_or_AX = pow(10, length - 2);

if ((length == 13 || length == 16) && (((credit_num - (credit_num % pe_visa)) / pe_visa) == 4))
printf("VISA\n");
else if ((length == 16) && ((((credit_num - (credit_num % pe_master_or_AX)) / pe_master_or_AX) >= 51) && (((credit_num - (credit_num % pe_master_or_AX)) / pe_master_or_AX) <= 55)))
printf("MASTERCARD\n");
else if ((length == 15) && ((((credit_num - (credit_num % pe_master_or_AX)) / pe_master_or_AX) == 34) || (((credit_num - (credit_num % pe_master_or_AX)) / pe_master_or_AX) == 37)))
printf("AMEX\n");
else
printf("INVALID\n");
}
// if the last of the sum is not zero then...
else
printf("INVALID\n");
}
// if the length was deffrient than what was spesfecated then...
else
printf("INVALID\n");
}
``````
• no need to use llabs, a negative number means theres an error. Commented Sep 11, 2023 at 14:44
• you need to actually ask a question. what do you think of my code is very subjective. Mostly what I think is it it is unnecessarily duplicative and convoluted. for example instead of using log10 again for checking if the multiple of 2 of the digit is longer than 1 digit, just check if its greater then 9. the largest possible value for a single doubled digit is 18 for the case of 9 so you dont need to worry about the value being greater than 20. if its greater than 9 add 10. simple. not sure why you need two seperate loops either. you know every other digit goes to the other accumulator Commented Sep 11, 2023 at 14:59
• thank you UpAndAdam for your Suggestions. Commented Sep 17, 2023 at 7:39