Thread: Why my program does not show correct results? it is showing -21474.....

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Why my program does not show correct results? it is showing -21474.....

    I need to get the correct answer for the following program for two integer ( one must be large like b)
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    
     int long a, b, results;
     a=1234;
     b=19887815769532909;
     results= (0.5)*(a+b)*(a+b+1)*b;
     printf("%d",results);
     getch();
    
    }
    please help.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    This code seems suspicious. What compiler and what OS are you using?

    "19887815769532909" doesn't fit inside a 32-bit integer, and your "long int" probably is 32-bit, seeing the results you're getting. Use "long long" instead, assuming your compiler supports it.
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    First of all, main() should be defined as:

    Code:
    int main(void)
    {
       // ...
    }
    "int long" should be just "long int" or better "long".

    your prinf() statement for printing a long should be:
    Code:
    printf("%ld",results);
    or if you use a "long long":
    Code:
    printf("%lld",results);
    Please stop using "conio.h" functions. They are Windows only and are NOT part of ANY C Standards!

    Your expression should result in a double, not a long, since you are multiplying with "(0.5)". Assigning it to a long truncates the fractional part.

    What is the answer you expect?

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The answer won't fit in a 64-bit int, either. You could try a long double, but the answer won't be exact. If you need exact integer results then you need to use a bignum library (or implement the operations yourself).

    The maximum unsigned 64-bit int is
    18446744073709551615

    The actual answer to your calculation is
    3933066314831012816960565291086094692643810092064

    The answer I get with a long double is
    3933066314831012817088639772748881198425864929280
    which is pretty pathetic (basically 64-bits precision, but with a floating binary point, of course).
    Code:
    #include <stdio.h>
    
    int main()
    {
      long double a, b, result;
      a = 1234;
      b = 19887815769532909;
      result = 0.5 * (a + b) * (a + b + 1) * b;
      printf("%.0Lf", result);
    
    //  getchar();
      return 0;
    }
    Last edited by algorism; 03-07-2017 at 05:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-10-2014, 08:52 PM
  2. Replies: 20
    Last Post: 07-09-2014, 02:48 AM
  3. Show results in command prompt (MFC)
    By myle in forum Windows Programming
    Replies: 1
    Last Post: 05-15-2007, 12:35 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. show all compiler results?
    By bluehead in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2002, 10:57 PM

Tags for this Thread