|
| 1 | +## Dimik Even or Odd 2 |
| 2 | + |
| 3 | +### What the problem Wants |
| 4 | +In this problem, you will be given `T` testcases.Each line of the testcase consist of an integer `n`.We have to determine whether `n` is even or odd. The difference from dimik even odd 1 is that the number can have upto 100 digits which won't fit in long long data type of C++. |
| 5 | + |
| 6 | +### Approach |
| 7 | +- Take Input as a string so that it can take upto 100 digits |
| 8 | +- Check the last digit of that string |
| 9 | +- If its divisible by 2 then print 'even' otherwise print 'odd' |
| 10 | + |
| 11 | +### Solution in C++ |
| 12 | + |
| 13 | +> Step 1: Include necessary libraries |
| 14 | +
|
| 15 | +```cpp |
| 16 | +#include<bits/stdc++.h> |
| 17 | +using namespace std; |
| 18 | +``` |
| 19 | + |
| 20 | +The bits/stdc++.h library is included, which is a header file that includes all the standard libraries in C++. |
| 21 | + |
| 22 | +### Define the function to check if a number is even |
| 23 | + |
| 24 | +```cpp |
| 25 | +bool isEven(string number) { |
| 26 | + int lastDigit = number[number.size() - 1] - '0'; |
| 27 | + return (lastDigit % 2 == 0); |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +> Step 2: Function for identify Odd Even |
| 32 | +
|
| 33 | +- Inside the isEven function, we extract the last digit of the number by accessing the last character of the string (number[number.size() - 1]) and subtracting the ASCII value of '0' from it. This converts the character representation of the digit to an integer. |
| 34 | +
|
| 35 | +- We then check if the last digit is divisible by 2 using the modulo operator (%). If the remainder is 0, the number is even and we return true; otherwise, we return false |
| 36 | +
|
| 37 | +> Step 3: Use the function from main |
| 38 | +
|
| 39 | +### Implement the main function |
| 40 | +```cpp |
| 41 | +int main() { |
| 42 | + int numTestCases; |
| 43 | + cin >> numTestCases; |
| 44 | + cin.ignore(); // Ignore the newline character after numTestCases |
| 45 | +
|
| 46 | + for (int i = 0; i < numTestCases; i++) { |
| 47 | + string number; |
| 48 | + getline(cin, number); |
| 49 | + if (isEven(number)) { |
| 50 | + cout << "even" << endl; |
| 51 | + } else { |
| 52 | + cout << "odd" << endl; |
| 53 | + } |
| 54 | + } |
| 55 | +
|
| 56 | + return 0; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +- In the main function, we first read the number of test cases from the input using cin >> numTestCases. |
| 61 | + |
| 62 | +- Since we read an integer before reading strings, we need to ignore the newline character left in the input buffer. We do this using cin.ignore(). |
| 63 | + |
| 64 | +- Next, we enter a loop to process each test case. For each test case, we read the number as a string using getline(cin, number). This allows us to handle numbers with multiple digits. |
| 65 | + |
| 66 | +- We then call the isEven function with the number as the argument. Depending on the return value, we output whether the number is even or odd using cout. |
| 67 | + |
| 68 | +### Full code |
| 69 | +```cpp |
| 70 | +#include<bits/stdc++.h> |
| 71 | +using namespace std; |
| 72 | + |
| 73 | +bool isEven(string number) { |
| 74 | + int lastDigit = number[number.size() - 1] - '0'; |
| 75 | + return (lastDigit % 2 == 0); |
| 76 | +} |
| 77 | + |
| 78 | +int main() { |
| 79 | + int numTestCases; |
| 80 | + cin >> numTestCases; |
| 81 | + cin.ignore(); // Ignore the newline character after numTestCases |
| 82 | + |
| 83 | + for (int i = 0; i < numTestCases; i++) { |
| 84 | + string number; |
| 85 | + getline(cin, number); |
| 86 | + if (isEven(number)) { |
| 87 | + cout << "even" << endl; |
| 88 | + } else { |
| 89 | + cout << "odd" << endl; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | +``` |
| 96 | +
|
| 97 | +### Solution in python |
| 98 | +
|
| 99 | +As python can take upto 100 digit as input we will just check if its even or odd by just dividing it by 2 |
| 100 | +
|
| 101 | +```py |
| 102 | +testCase=eval(input()) # taking test case and eval converting it to int |
| 103 | +for i in range(testCase): # for each test case |
| 104 | + num=eval(input()) # take the numnber and convert it to int |
| 105 | + if(num%2==0): # check even or odd |
| 106 | + print('even') |
| 107 | + else: |
| 108 | + print('odd') |
| 109 | +``` |
0 commit comments