-4

I need to make a program that can take numbers of up to 100 digits as input. No standard int datatype will be able to do that! I've never come across such a bizarre situation.

I don't get it at all. How am I supposed to solve this?

The question I'm working on is this:

A whole number will be given, and you have to make a program that will determine whether it's an even or odd number.

Input Specification

In the first line, there will be an integer T denoting the number of testcases. In the following T lines, a non-negative integer will be given. The number can have a maximum of 100 digits.

Output Specification

For every whole number given, you will have to print whether it's odd or even as output.

Can anyone guide me on how to solve the problem (if it is even possible to do so)?

Soha Farhin Pine
  • 313
  • 6
  • 16
  • no standard datatype will be able to do, just FYI. – Sourav Ghosh Apr 20 '16 at 11:20
  • What operations do you want to perform on this number? – Mohit Jain Apr 20 '16 at 11:20
  • `log_2(10) ~ 3.32` – EOF Apr 20 '16 at 11:21
  • _The question is in Bengali. Therefore, most of the people here won't understand the question._ yes probably... not really necessary to mention this. – Jabberwocky Apr 20 '16 at 11:36
  • Can most of you delete/edit your comments? I have changed the question to meet the required standard. – Soha Farhin Pine Dec 19 '16 at 04:14
  • In this case, the accepted solution is correct; you only need to look at the last digit. You did state however there is no way to work with really large numbers which is incorrect, it takes a bit of heavy lifting -- however you don't need to do the dirty work as there are libraries for whichever flavor of C/C++ you use. See : http://stackoverflow.com/questions/565150/bigint-in-c – Kraang Prime Dec 20 '16 at 08:57
  • The program will take a number as input and determine whether it's odd or even. – Soha Farhin Pine Dec 20 '16 at 09:03
  • @SamuelJackson Thanks for the additional information. I did not know we _can_ work with very large numbers. – Soha Farhin Pine Dec 20 '16 at 09:06

4 Answers4

11

The program will take a number as input and determine whether it's odd or even.

Read the input in a string (char [101]) and analyze only last digit to check whether number is odd or even. Rest of the digits are irrelevant for this task.

Soha Farhin Pine
  • 313
  • 6
  • 16
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • 3
    For completeness: Here "*last*" means "least significant". – alk Apr 20 '16 at 11:50
  • @chux :) I once reached 45% on [this one](http://stackoverflow.com/questions/29295427/why-is-a-fork-call-not-optimized-away-in-an-infinite-loop/29296483#29296483) too. – Mohit Jain Apr 24 '16 at 09:16
  • 1
    Can you people please upvote this question? It has been thoroughly edited to meet the standard. – Soha Farhin Pine Dec 25 '16 at 17:26
3

There is no standard numeric type guaranteed to hold that many digits. You need to store the value in a different way, e.g., as a string or other array. If you need to perform arithmetic on these numbers, you need to implement those operations for the types you use, or use some kind of arbitrary precision library.

(Tip: You also don't necessarily need the entire number for certain operations, e.g., you can tell whether it is even or odd by looking only at the last digit…)

Arkku
  • 41,011
  • 10
  • 62
  • 84
3

The exercise is to determine whether a whole number of up to 100 digits is odd or even.

This does not require you to perform arbitrary arithmetic on the number, so if you need to handle numbers larger than the largest integer type on your system, you can treat them as a string of digits.

Whether it is even or odd only depends on the last digit.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
1

To all those who took the time and effort to answer this question,

Thanks for the answers. And thanks for showing the way. I greatly appreciate the help!


The solution to the problem which I have coded is -

#include <stdio.h>
#include <string.h>

int main()
{
    int T, i, j;
    scanf("%d", &T);

    for (i=1; i<=T; i++)
    {
         char N[101];
         scanf("%s", N);
         int k = strlen(N);
         int p = N[k-1] - 48; //char to int conversion

         if (p % 2 == 1)
         {
             printf("odd\n");
         }

         else 
         {
             printf("even\n");
         }
     }

     return 0;
}
Soha Farhin Pine
  • 313
  • 6
  • 16
  • Using scanf is a bit dangerous as it will overflow the buffer if more than 101 characters are entered ( http://stackoverflow.com/a/7709467/1527 ). Check the result from strlen for zero to avoid another buffer overflow. You also might want to check that the contents of N are actually digits - is 'aaaaa' even? It's possibly better to use atoi rather than subtracting 48 as C doesn't guarantee ASCII character encoding ( though I don't know of one where the even digits aren't even ), or just skip that step and test whether the character is even or odd. – Pete Kirkham Apr 26 '16 at 09:16
  • Well, it is guaranteed that the given string wholly consists of upto 100 numerical digits. Therefore, I don't need to go through that trouble. – Soha Farhin Pine May 06 '16 at 15:23
  • 1
    It's better not to get into bad habits which you have to relearn when you do real-world work. – Pete Kirkham May 06 '16 at 15:27