简体   繁体   English

当我在本地 vscode 中使用我的代码时,它只给出 63 作为答案,但是当我使用不同的在线编译器时它工作正常

[英]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 .我的输入就像374245455400126378282246310005 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.

相关问题 为什么如果我使用cin时此代码在c ++中能正常工作,但在c中使用scanf时却失败? - why this code works fine in c++ if i use cin but it fails when i use scanf in c? 为什么我的代码只在我使用 printf 时运行? - Why does my code only run when I use printf? 当我认为我的代码很好时,Turbo C会给我“声明终止错误” - Turbo C is giving me “Declaration Terminated Incorrectly” when I believe my code is fine 代码在我的系统上运行良好,但是当我将代码提交给应该检查它的机器人时会导致堆栈粉碎错误 - Code works fine on my system but causes a stack smashing error when i submit it to the bot that is supposed to check it 我的代码有什么问题。 我不需要 output。 当在其中一个在线编译器中执行此代码时,不需要 output - what is wrong with my code. I am not getting required output. when in executed this code in one of the online compiler it is no giving required output 为什么我应该在“char bigchar[ 1u &lt;&lt; 31 - 1 ];”时使用 malloc(); 工作正常吗? - Why should I use malloc() when "char bigchar[ 1u << 31 - 1 ];" works just fine? list.h语法错误,只有在我使用我的C项目中的代码时才会出现 - list.h syntax errors that appear only when I use the code in a my C project 为什么在printf工作正常时使用stderr? - Why use stderr when printf works fine? 为什么当我想在下面的代码中创建新的数据集时,当我使用true算法时却无法正常工作 - Why when I want to create a new dataset in code below it works wrong when I use the the true algorithm 当我使用printf()时,多线程代码中的结果不同 - Different results in multithread code when I use printf()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM