C 语言的 Luhn 算法
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论