C 语言的 Luhn 算法

发布于 2025-01-12 20:52:54 字数 990 浏览 4 评论 0原文

我在 cs50 中使用 C 中的 Luhn 算法,我们还没有使用数组,所以我考虑了另一种方法。我认为我的代码很好,但是当我输入信用卡号时它不会输出任何内容,它只会返回到该行并且不会打印任何内容。

#include <stdio.h>

int main()
{
    
    long long num, n;
    int result, sum, temp;
    
    
     printf("please enter your credit card number\n");
    scanf("%lld", &num);
    
    
    
    sum = 0;
    sum = num % 10;
    num = num / 10;
    
    num = n;
    
    while (num >10)
    {
        temp = ( num % 10) * 2;
        if (temp > 9)
        {
            temp = temp - 9;
        }
        sum = sum + temp; 
        num = num / 10; 
        temp = num%10; 
        sum = sum + temp;
        num = num / 10;
    }
        
    
    
     while ( n > 10);
     
         
         n = n/10;
         
         if (sum%10 == 0)
         
         { if (n==4)
            printf("VISA");
            else if (n==3);
            printf("AMEX");
         }
         else printf("INVALID");
     
    }

 

我错过了一些东西,但我不明白它是什么。

I am using the Luhn algorithm in C for cs50 , we don't use array yet so I thought about another method. I think my code is fine but it doesn't output anything when I enter a credit card number it just goes back to the line and doesn't print anything.

#include <stdio.h>

int main()
{
    
    long long num, n;
    int result, sum, temp;
    
    
     printf("please enter your credit card number\n");
    scanf("%lld", &num);
    
    
    
    sum = 0;
    sum = num % 10;
    num = num / 10;
    
    num = n;
    
    while (num >10)
    {
        temp = ( num % 10) * 2;
        if (temp > 9)
        {
            temp = temp - 9;
        }
        sum = sum + temp; 
        num = num / 10; 
        temp = num%10; 
        sum = sum + temp;
        num = num / 10;
    }
        
    
    
     while ( n > 10);
     
         
         n = n/10;
         
         if (sum%10 == 0)
         
         { if (n==4)
            printf("VISA");
            else if (n==3);
            printf("AMEX");
         }
         else printf("INVALID");
     
    }

 

I am missing something, but I don't see what it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文