[英]When i use my code in local vscode its giving only 63 as answer but when i use a different online complier it works fine
This is a Luhn algorithm code and it works fine in an online complier but when I use it in my local vscode it is only giving 63 as output.这是一个 Luhn 算法代码,它在在线编译器中运行良好,但是当我在本地 vscode 中使用它时,它只给出 63 作为 output。
I dont know if its a memory issue as it late long variable.我不知道它是否是 memory 问题,因为它是后期长变量。
ie credit card number as input.即信用卡号作为输入。
#include <stdio.h>
// Finds its Luhn algorithm to see if its a valid credit card number.
void checksum(long num)
{
int sum = 0;
for (int i = 0; num != 0; num /= 10, i++)
{
if (i % 2 == 0)
{
sum = sum + num % 10;
}
else
{
int digit = 2 * (num % 10);
sum = sum + (digit / 10) + (digit % 10);
}
}
printf("%d", sum);
}
int main()
{
long int num;
// Takes credit Card number as input.
do
{
printf("Number: ");
scanf("%li", &num);
} while (num < 0);
checksum(num);
return 0;
}
My inputs are like 374245455400126
, 378282246310005
.我的输入就像374245455400126
、 378282246310005
。 And output is always 63
.而 output 总是63
。
The result depends on the size of the type long int
that can be equal either to the size of the type int
or to the size of the type long long int
.结果取决于long int
类型的大小,该大小可以等于int
类型的大小或long long int
类型的大小。
So use the type long long int
instead of the type long int
.所以使用long long int
类型而不是long int
类型。
Also as the program expects an unsigned value then instead of the signed type long long int
it is even better to use the type unsigned long long int
.此外,由于程序需要一个无符号值,因此使用unsigned long long int
类型而不是有符号类型long long int
会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.