Thread: is it possible?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    44
    Code:
    if((first/second)!=int)
    is it possible?? I want to get result is int or not??

    because result can be float..
    Last edited by koyboy; 05-24-2008 at 08:32 AM.

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Code:
    if((first%second)!=0)
    I think this is how it will be done. Keep in mind that 'first' and 'second' should be int for this.

    Don't use my code if its a nuclear project you're doing though, because 70% of my code is wrong.
    Last edited by abh!shek; 05-24-2008 at 08:38 AM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    44
    thanks for reply

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C is a strongly typed language. If you divide an int with an int, you will get an integer back and nothing else.
    The result will never be a float UNLESS you divide an integer with a float in which case you WILL get a float back.
    If you want to check if it can be represented in an integer (ie, no decimals), then you can try something like
    first % (int)first == 0
    Last edited by Elysia; 05-24-2008 at 09:23 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    44
    I know it.. but result is false..Like this:
    1)first and second value is random (Example first:15 second:10)
    2)ask user first/second
    3)if user enter 1 its correct in programming but it isnt correct in real math..

    so I want to create first and second value which can be something that is divisible by one another..

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    44
    I write like this and thats ok...

    Code:
    second = (rand()%99)+1
    do{
          	first = second * ((rand()%10)+1);
          }while(first>=100);
    and if I ask first/second, result is exactly int in real and in programming and first%second=0

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by koyboy View Post
    and if I ask first/second, result is exactly int in real and in programming and first%second=0
    I see no problem with this..
    first%second gives remainder, I think you know that?

    So if first%second==0, that means the remainder is zero and quotient is an int. Same as what "real" math says.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by abh!shek View Post
    Don't use my code if its a nuclear project you're doing though, because 70% of my code is wrong.
    Funny you should say that. I've actually written code that one place it runs is a nuclear facility.
    Aparently when our system goes down, a loud siren can be heard for miles... according to one of our guys that was sent over there once.
    I'm not kidding.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are first & second defined as ints or as floats or doubles?

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    @cpjust in think, they are all int, since they are generated by rand as per what Op says.


    ssharish

Popular pages Recent additions subscribe to a feed