Result for number aboove its range

Discussion in 'C' started by sakshi02, Aug 30, 2008.

  1. sakshi02

    sakshi02 New Member

    Joined:
    Aug 30, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hey can anyone tell me, that if i want to make a program which test whether a no is prime or nt for all integers and also for nos above the range of integer. Is there any way. Do i need to set my own range. pls tell me a way.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    There are no (finite) numbers beyond the range of integer. Do you mean the int type? If so you may be able to extend the range by using long instead of int. Check your compiler documentation for how large the different integer types are; the standard only mandates that short <= int <= long, so all integer types could be 32-bit.

    If no built in types fit the range you want to test then you'll have to define your own type and encode the operations you want to use. This isn't as difficult as it sounds; there's already an example of how we normally do that in this reply, i.e. the number 32 - no single digit has a value greater than 9, so we use multiple digits to represent numbers larger than 9. So you can use multiple ints to represent numbers larger than 2^(8*sizeof int), for example int bignum[4] will represent a number up to 2^(4*8*sizeof int).

    Then you will need to write code to add these, because the built in operators only know about built in types. Again this is fairly easy; just remember how you did it on paper then write code to do that, for example:
    1234
    5678+
    ====

    4+8=12, so write 2 below 4 and 8 and carry the 1. 3+7=10, add the carry=11, write 1, carry 1.
    2+6=8, add carry=9, write 9. 1+5=6.
    So in code you might have something like (simplified):
    Code:
    int bignum1[4]={1,2,3,4};
    int bignum2[4]={5,6,7,8};
    int bugnum3[4]={0,0,0,0}; // this will contain the sum
    int carry=0;
    for (int i=3; i>=0; i--) {
      bignum3[i]=bignum1[i]+bignum2[i]+carry;
      carry=0;
      if (bignum3[i]>10) {
        bignum3[i]-=10;
        carry=1;
      }
    }
    
     
  3. sakshi02

    sakshi02 New Member

    Joined:
    Aug 30, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    What i did is I create a func atoi1 as i want in long long
    so i wrote
    unsigned long long atoi1(char s[])
    {
    unsigned long long m=0;
    int i;
    for(i=0;s>='0' && s<='9';++i)
    m=10*m+(s-'0');
    return m;
    }
    in main i create an array which takes the no string from user
    and with the help of this func it converts it into long long
    again it is giving negative result on exceeding the value from 32767
    ie 16 bit
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How are you displaying the return value?
    I tried the above code and it worked fine with 1048576, using Visual Studio 2005.
     
  5. sakshi02

    sakshi02 New Member

    Joined:
    Aug 30, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    i did in TC. and i wrote printf("%d",m);
    oh i should try %ld, is this the prob. let me check it on visual studio
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice