gpt4 book ai didi

c - int 中使用前导零

转载 作者:行者123 更新时间:2023-11-30 20:09:16 25 4
gpt4 key购买 nike

我正在尝试完成一个程序,但是当它作为 int 读取时,前导零被删除。如果用户在开始时输入零,我需要这个前导零,因为我在程序中使用它进行数学运算,而不能只在 printf 中添加前导零。

printf("Enter the first 6 digits of the barcode: \n");
scanf("%i", &n1);
printf("Enter the first 6 digits of the barcode: \n");
scanf("%i", &n2);

//Splits number1 into individual digits
count1 = 0;
while (n1 != 0){
array1[count1] = n1 % 10;
n1 /= 10;
count1++;
}

count2 = 0;
while (n2 > 0){
array2[count2] = n2 % 10;
n2 /= 10;
count2++;
//Steps 1-3
int sumo = array1[5]+array1[3]+array1[1]+array2[5]+array2[3]+array2[1]; //adds odd
int sume = array1[4]+array1[2]+array1[0]+array2[4]+array2[2]; //adds even without 12
int sumd = 3*sumo; //multiplies odds
int sum = sume+sumd; //adds above and evens
int chec = sum%10;
int check = 10-chec;

整个程序可以找到here

最佳答案

当您将值存储为整数时,前导零总是会丢失,因此您需要将值存储为其他内容(可能是字符串)

关于c - int 中使用前导零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53256203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com