Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Program that determines if a number is odd or even shows the wrong output if number is longer than 5 digits

If I enter a number that's more than 5 digits long, it shows the wrong number. For example:

Enter Integer: 123456
-7616 is EVEN.-7616 is ODD.

My teacher wants us to use Turbo C++ but it sometimes freezes for me after I run a program so I used OnlineGDB (https://www.onlinegdb.com) (language: C (TurboC)) instead. Here is my code:

#include <stdio.h>
#include <conio.h>

int number;

int main()
{
    clrscr();
    
    printf("Enter Integer: ", number);
    scanf("%d", &number);
    
    if ((number%2)==0)
    {
        printf("%d is EVEN.", number);
    }
    
    printf("%d is ODD.", number);
    
    getch();
    return(0);
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It looks like int is a 16-bit integer. 123456 is greater than the 16-bit signed integer limit (32767), so scanf() can’t put the whole number into number. Instead, it gets truncated. 123456 is represented in binary as 11110001001000000. That’s 17 bits, but only the last 16 can fit in number. With the leftmost bit gone, we have 1110001001000000, which is -7616 in two’s complement form (which is what an integer uses).

Try using a larger integer type, like long.

As others suggested in the comments, you may want to put the printf() for odd numbers in an else.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!