Typical int
(signed 32-bit long) can store only upto 2,147,483,647. Your input 2345678915
is beyond this.
If this is supported in your environment, you can use long long
, which is typically 64-bit long and can store upto 9,223,372,036,854,775,807.
#include <stdio.h>
int main()
{
long long id;
printf("Enter the Id: ");
scanf("%lld", &id);
if(id % 2 == 0)
printf("%lld is even.\n", id);
else
printf("%lld is odd.\n", id);
return 0;
}
Another option is using int64_t
, which may be supported in wider environments.
#include <stdio.h>
#include <inttypes.h>
int main()
{
int64_t id;
printf("Enter the Id: ");
scanf("%" SCNd64, &id);
if(id % 2 == 0)
printf("%" PRId64 " is even.\n", id);
else
printf("%" PRId64 " is odd.\n", id);
return 0;
}
Being inspired from @0___________, you can judge if a number is even or odd by just seeing the last digit.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int input;
int is_valid_number = 1;
size_t input_len = 0;
char* id = malloc(1);
if (id == NULL) {
perror("malloc");
return 1;
}
printf("Enter the Id: ");
/* read the first line */
while ((input = getchar()) != '\n' && input != EOF) {
char* next_id = realloc(id, input_len + 2); /* +1 for new character and +1 for NUL */
if (next_id == NULL) {
perror("realloc");
free(id);
return 1;
}
/* check if the input correctly represents a number */
is_valid_number = is_valid_number && (isdigit(input) || (input_len == 0 && input == '-'));
/* store this digit */
id = next_id;
id[input_len++] = (char)input;
}
id[input_len] = '\0';
if (input_len == 0 || !is_valid_number)
printf("%s is not a number.\n", id);
else if((id[input_len - 1] - '0') % 2 == 0)
printf("%s is even.\n", id);
else
printf("%s is odd.\n", id);
free(id);
return 0;
}