英文:
My EVEN ODD Program giving odd output for every 10 digit even number
问题
我创建了一个奇偶数检测程序来检测奇数和偶数。对于所有小于10位数的数字,它都正常工作,但当我在代码中输入2222222222时,它输出奇数,为什么?
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
if(a%2==0){
cout << "这是偶数";
}
else{
cout << "这是奇数";
}
return 0;
}
我尝试输入2222222222,期望输出偶数,但它输出了奇数。
英文:
I created odd even program to detect odd and even numbers. It is working fine for all numbers less than 10 digits but when I give input of 2222222222 in the code, it gives the output of odd but how?
#include<iostream>
using namespace std;
int main(){
int a;
cin>>a;
if(a%2==0){
cout<<"This is EVEN";
}
else{
cout<<"This is ODD";
}
return 0;
}
I tried giving input of 2222222222 and expected the output of even but it gave odd.
答案1
得分: 2
我猜你正在使用32位,因此整数范围从2,147,483,647到-2,147,483,648。使用另一种数据类型(例如 long long
)或切换到64位。
编辑:正如@wohlstad在他的评论中所说,切换到64位可能无法解决问题。使用另一种数据类型是正确的方法。
英文:
I guess you are using 32 Bit and thus, the integer ranges from 2 147 483 647 to -2 147 483 648. Use another datatype (e.g. long long
) or switch to 64 Bit.
EDIT: As @wohlstad said in his comment, switching to 64 Bit might not solve the problem. Using another datatype is the way to go.
答案2
得分: 1
问题在于 2222222222 大于 int
类型的最大值(即 2147483647)。因此会导致溢出,并导致错误的输出。
解决此问题的几种方法。最简单的方法是使用 long long
类型,它具有较大的范围(在32位和64位X86变体中都可以达到 9223372036854775807)。
像这样:
#include <iostream>
#include <limits>
int main()
{
long long int a;
std::cout << "Enter a number in range "
<< 0
<< " to "
<< std::numeric_limits<decltype( a )>::max()
<< ": ";
std::cin >> a;
if ( a % 2 == 0 )
std::cout << "This is EVEN\n";
else
std::cout << "This is ODD\n";
}
示例输出:
Enter a number in range 0 to 9223372036854775807: 2222222222
This is EVEN
另一种方法是将输入作为 std::string
并检查其最低有效位是否为偶数。
这是一个示例:
#include <iostream>
#include <locale>
#include <string>
#include <string_view>
bool is_number( const std::string_view str )
{
const auto loc { std::cout.getloc() };
for ( const char c : str )
if ( !std::isdigit( c, loc ) ) return false;
return true;
}
int main()
{
std::string a;
do
{
std::cout << "Enter a number as big as you want: ";
std::cin >> a;
}
while ( !is_number( a ) );
if ( a.back() % 2 == 0 )
std::cout << "This is EVEN\n";
else
std::cout << "This is ODD\n";
}
示例输出:
Enter a number as big as you want: 2222222222222222222222222222222222222222222222222222222222222222222222222222222222229
This is ODD
现在请注意,在上述解决方案中,条件 if ( a.back() % 2 == 0 )
可能看起来奇怪,因为 a.back()
返回一个 char
,然后我们对其应用模(%
)运算符。这是有效的,因为偶数十进制数字的ASCII值也是偶数的(例如,'0' == 48,'2' == 50,'4' == 52)。因此,最终结果是保证正确的。
英文:
The problem is that 2222222222 is greater than the max value for int
(i.e. 2147483647). Thus it causes an overflow and it results in a wrong output.
There are a few ways to solve this problem. The easiest one is to use long long
which has a large range (up to 9223372036854775807 in both 32 and 64 bit X86 variants).
Like this:
#include <iostream>
#include <limits>
int main()
{
long long int a;
std::cout << "Enter a number in range "
<< 0
<< " to "
<< std::numeric_limits<decltype( a )>::max()
<< ": ";
std::cin >> a;
if ( a % 2 == 0 )
std::cout << "This is EVEN\n";
else
std::cout << "This is ODD\n";
}
Sample output:
Enter a number in range 0 to 9223372036854775807: 2222222222
This is EVEN
Another way of doing it is to get the input as a std::string
and then check to see whether or not its least significant digit is even.
This is an example:
#include <iostream>
#include <locale>
#include <string>
#include <string_view>
bool is_number( const std::string_view str )
{
const auto loc { std::cout.getloc() };
for ( const char c : str )
if ( !std::isdigit( c, loc ) ) return false;
return true;
}
int main()
{
std::string a;
do
{
std::cout << "Enter a number as big as you want: ";
std::cin >> a;
}
while ( !is_number( a ) );
if ( a.back() % 2 == 0 )
std::cout << "This is EVEN\n";
else
std::cout << "This is ODD\n";
}
Sample output:
Enter a number as big as you want: 2222222222222222222222222222222222222222222222222222222222222222222222222222222222229
This is ODD
Now note that in the above solution, the condition if ( a.back() % 2 == 0 )
may seem weird because a.back()
returns a char
and then we're applying the modulo (%
) operator on it. This is valid since the ASCII values for even decimal digits are even as well (e.g. '0' == 48, '2' == 50, '4' == 52). So in the end, the result is guaranteed to be correct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论