there is a problem with my luhn checksum in cs50 that I can’t figure out

this the cs50 problem in question :
credit

the logic in my checksum seem to be ill constructed as it doesn’t recognize 4222222222222 as valid even though it sums is 40 and falsely indicates
4111111111111113 as visa even though it sums is 28
other than these two it labels any other credit card correctly

this is my code :

#include <cs50.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>

bool digit(string str, int buffer);
int numofdigits(long long card_num);
int fst_ndigit(long long credit, int buffer, int numdigit);

int main(void)
{

    long long credit = get_long("please enter your credit card number :  ");

    //Note:
    //using a boolean variable can reduce repetition of " INVALID " message
    bool valid = true;
    if (credit < (long long) pow(10, 13) || credit > (long long) pow(10, 16))
    {
        printf("INVALIDn");
        valid = false;
    }


    int buffer = numofdigits(credit);

    long long aux = credit;
    int last_two;
    int last_one;
    //Note: variable names cannot start with numbers!
    int secondlast_one;
    int mul = 1;
    int sum1 = 0;
    int sum2 = 0;

    if(valid){
    for(int i = 0; aux > 0; i++)
    {
        //1st step :
        //Multiply every other digit by 2, starting with the number’s second-to-last digit,
        //and then add those products’ digits together.

        last_two = aux % (long long) pow(10, buffer-2);
        last_one = last_two % 10;
        secondlast_one = last_two / 10;
        mul = secondlast_one*2;

        if((mul / 10) != 0)
        {
            sum1 += (mul / 10) + (mul % 10);
        }
        else
        {
            sum1 += mul;
        }
        //2nd step:
        //Add the previous sum to the sum of the digits that weren’t multiplied by 2.
        sum2 += last_one;

        aux = aux / 100;
    }

    //3rd step:
    //If the total’s last digit is 0
    //(or, put more formally, if the total modulo 10 is congruent to 0),
    //the number is valid!
    if((sum1 + sum2) % 10 ==0)
    {
        valid = true;
    }


    int fst_two = fst_ndigit(credit, buffer, 2);
    int fst_one = fst_ndigit(credit, buffer, 1);

    char str[17];
    // Note :
    // sprintf(str, "%d", (int)credit);
    // this line is incorrect because the prog will spit out a random integer

    sprintf(str, "%lld", credit);
    // lld is format for long long int


    if (digit(str, buffer) && valid)
    {
        if (buffer == 15 && (fst_two == 34 || fst_two == 37))
        {
            printf("AMEXn");
        }
        else if (buffer == 16 && (fst_two == 51 || fst_two == 52 || fst_two == 53 || fst_two == 54 || fst_two == 55))
        {
            printf("MASTERCARDn");
        }
        else if ((buffer == 13 || buffer == 16) && fst_one == 4)
        {
            printf("VISAn");
        }
        else
        {
            printf("INVALIDn");
        }
    }
    else
    {
        printf("INVALIDn");
    }
    }

}


int numofdigits(long long card_num)
{
    int count = 0;
    while (card_num > 0)
    {
        card_num /= 10;
        count++;
    }
    return count;
}

bool digit(string str, int buffer)
{
    bool all_digits = true;
    for (int i = 0; i < buffer; i++)
    {
        if (!isdigit(str[i]))
        {
            all_digits = false;
            break;
        }
    }
    return all_digits;
}

int fst_ndigit(long long credit, int buffer, int numdigit)
{
    int fstndigit = (int) (credit / (long long) pow(10, buffer - numdigit));
    return fstndigit;
}

as you see in the checksum section:

int buffer = numofdigits(credit);

    long long aux = credit;
    int last_two;
    int last_one;
    //Note: variable names cannot start with numbers!
    int secondlast_one;
    int mul = 1;
    int sum1 = 0;
    int sum2 = 0;

    if(valid){
    for(int i = 0; aux > 0; i++)
    {
        //1st step :
        //Multiply every other digit by 2, starting with the number’s second-to-last digit,
        //and then add those products’ digits together.

        last_two = aux % (long long) pow(10, buffer-2);
        last_one = last_two % 10;
        secondlast_one = last_two / 10;
        mul = secondlast_one*2;

        if((mul / 10) != 0)
        {
            sum1 += (mul / 10) + (mul % 10);
        }
        else
        {
            sum1 += mul;
        }
        //2nd step:
        //Add the previous sum to the sum of the digits that weren’t multiplied by 2.
        sum2 += last_one;

        aux = aux / 100;
    }

    //3rd step:
    //If the total’s last digit is 0
    //(or, put more formally, if the total modulo 10 is congruent to 0),
    //the number is valid!
    if((sum1 + sum2) % 10 ==0)
    {
        valid = true;
    }

in a loop that goes on until all digits of the credit card number is checked, I tried to extract the last digit and the second to last then multiplied the latter by 2 and added to sum1 and if it surpasses 9, I add each of its digits indiviually then sum2 is the sum of every other digit beginnig with the last digit

finally out of the loop:

if total = sum1 + sum2 have as last digit 0 then the credit car number is valid other than that invalid

can someone please help me ?

New contributor

abeginner1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật