Problem in solving even-odd number
You are given a nonnegative integer number. The number contains not more than 100 digits. Then you have to check if the number is even or odd.
I wrote the following code for this problem..but still getting wrong answer. :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
getline(cin,str);
if(str.length() <= 100)
{
unsigned long long num = atoi(str.c_str());
if(num%2==0)
cout<<"even"<<endl;
else
cout<<"odd"<<endl;
}
}
return 0;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
using namespace std;
int main()
{
int t,num;
cin>>t;
for(int i=1; i<=t; i++)
{
cin>>num;
if (num & 1)
cout << "odd" << endl;
else
cout << "even" << endl;
}
return 0;
}
|
Where is the problem? Why I am getting wrong answer?
You only ever need to examine the last digit in a number to see if it is even or odd.
There is no native integer type in C++ large enough to hold a 100 digit integer.
Topic archived. No new replies allowed.