如果大于10的位数,则不起作用

时间:2021-09-06 19:57:06

I want to make program that input number and print two numbers, one is all the odd digits and the second is all the even digits.

我想制作输入数字和打印两个数字的程序,一个是所有奇数,第二个是偶数。

it works fine if the number of the input's digits is less the 10 but when it gets number with 10 and more digits something get wrong. Do you know why?

如果输入数字的数字小于10,但是当它得到10和更多数字的数字时出错,它可以正常工作。你知道为什么吗?

unsigned long int n;
unsigned long int even=0,odd=0;
int countOdd=1,countEven=1;

printf("enter every number: \n");
scanf("%lu",&n);

 while(n!=0){
   if(n%2==0){
        even+=10*countEven*(n%10);
        countEven*=10;
              }
   else
       {
        odd+=10*countOdd*(n%10);
        countOdd*=10;

       }
   n=n/10;
   }
printf("the odd number is %d \n",odd/10);
printf("the even number is %d \n\n\n",even/10);

2 个解决方案

#1


4  

unsigned long is probably 32-bit on your machine, the biggest value that it can hold is 232 - 1 (4294967295), you are probably inputting a number that is bigger.

unsigned long在您的机器上可能是32位,它可以容纳的最大值是232 - 1(4294967295),您可能正在输入更大的数字。

Try unsigned long long instead, which is at least 64-bit.

尝试使用unsigned long long,这至少是64位。

#2


1  

You have declared n as unsigned long int which can hold maximum value of 4,294,967,295 in a 32 bit system. So when you enter the value which exceeds this the maximum value 4294967296 it outputs

您已将n声明为unsigned long int,它在32位系统中可以保存最大值4,294,967,295。因此,当您输入超过此值的值时,它将输出最大值4294967296

 odd 99795
 even 42462

#1


4  

unsigned long is probably 32-bit on your machine, the biggest value that it can hold is 232 - 1 (4294967295), you are probably inputting a number that is bigger.

unsigned long在您的机器上可能是32位,它可以容纳的最大值是232 - 1(4294967295),您可能正在输入更大的数字。

Try unsigned long long instead, which is at least 64-bit.

尝试使用unsigned long long,这至少是64位。

#2


1  

You have declared n as unsigned long int which can hold maximum value of 4,294,967,295 in a 32 bit system. So when you enter the value which exceeds this the maximum value 4294967296 it outputs

您已将n声明为unsigned long int,它在32位系统中可以保存最大值4,294,967,295。因此,当您输入超过此值的值时,它将输出最大值4294967296

 odd 99795
 even 42462