Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to check the result of division for an integer?

How to check the result of division for an integer?

Scheduled Pinned Locked Moved C#
tutorialquestion
11 Posts 7 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Khoramdin
    wrote on last edited by
    #1

    Hello everyone, I have two numbers A and B. I need to divide B by A and if the result is an integer then register A and B. Can someone tell me how I can check to see if the result of division is an integer or not? Thank you very much and have a great day. Khoramdin

    M V 2 Replies Last reply
    0
    • K Khoramdin

      Hello everyone, I have two numbers A and B. I need to divide B by A and if the result is an integer then register A and B. Can someone tell me how I can check to see if the result of division is an integer or not? Thank you very much and have a great day. Khoramdin

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      What is the datatype of variables "A" and "B"? int A = 3; int B = 6; int result = B / A; Did you write like that? if yes, result is INT. :) obviously. or Show us the code that you wrote.

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

      K 1 Reply Last reply
      0
      • M Michael Sync

        What is the datatype of variables "A" and "B"? int A = 3; int B = 6; int result = B / A; Did you write like that? if yes, result is INT. :) obviously. or Show us the code that you wrote.

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net) If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you. :)

        K Offline
        K Offline
        Khoramdin
        wrote on last edited by
        #3

        Hello Michael, Thanx for the reply, mate. I haven't written any code but what you have written is what I had in mind. The problem is how can I check to see if the result is NOT int. After all, I could have: int A = 4; int B = 6; double result = B / A; I simply wish to be able to knowwhen the result is integer and when it is not. Thank you very much and have a great day. Khoramdin

        R C L 3 Replies Last reply
        0
        • K Khoramdin

          Hello Michael, Thanx for the reply, mate. I haven't written any code but what you have written is what I had in mind. The problem is how can I check to see if the result is NOT int. After all, I could have: int A = 4; int B = 6; double result = B / A; I simply wish to be able to knowwhen the result is integer and when it is not. Thank you very much and have a great day. Khoramdin

          R Offline
          R Offline
          Rocky
          wrote on last edited by
          #4

          The result will always be an int when the remainder is zero so check to see if B%A == 0

          K C 2 Replies Last reply
          0
          • R Rocky

            The result will always be an int when the remainder is zero so check to see if B%A == 0

            K Offline
            K Offline
            Khoramdin
            wrote on last edited by
            #5

            Hello Rocky, Thank you very much, mate. That was EXACTLY what I was looking for. Have a great day. Khoramdin

            R 1 Reply Last reply
            0
            • K Khoramdin

              Hello Rocky, Thank you very much, mate. That was EXACTLY what I was looking for. Have a great day. Khoramdin

              R Offline
              R Offline
              Rocky
              wrote on last edited by
              #6

              anytime mate ur welcome...I'm glad ur happy

              1 Reply Last reply
              0
              • K Khoramdin

                Hello Michael, Thanx for the reply, mate. I haven't written any code but what you have written is what I had in mind. The problem is how can I check to see if the result is NOT int. After all, I could have: int A = 4; int B = 6; double result = B / A; I simply wish to be able to knowwhen the result is integer and when it is not. Thank you very much and have a great day. Khoramdin

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                While the mod operator solved your problem, it's worth mentioning that B/A will always be an int, unless A is ( or is cast to ) a float or double.

                Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                1 Reply Last reply
                0
                • K Khoramdin

                  Hello Michael, Thanx for the reply, mate. I haven't written any code but what you have written is what I had in mind. The problem is how can I check to see if the result is NOT int. After all, I could have: int A = 4; int B = 6; double result = B / A; I simply wish to be able to knowwhen the result is integer and when it is not. Thank you very much and have a great day. Khoramdin

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  One trick you try is to see if there is a remainder using mod (%). Thanks.

                  1 Reply Last reply
                  0
                  • K Khoramdin

                    Hello everyone, I have two numbers A and B. I need to divide B by A and if the result is an integer then register A and B. Can someone tell me how I can check to see if the result of division is an integer or not? Thank you very much and have a great day. Khoramdin

                    V Offline
                    V Offline
                    V 0
                    wrote on last edited by
                    #9

                    If A = 15 and B = 3 ==> A%B = 0 If A = 17 and B = 4 ==> A%B != 0

                    V. I found a living worth working for, but haven't found work worth living for.

                    1 Reply Last reply
                    0
                    • R Rocky

                      The result will always be an int when the remainder is zero so check to see if B%A == 0

                      C Offline
                      C Offline
                      Cfer83
                      wrote on last edited by
                      #10

                      I think you just gave him the answer to his homework assignment.

                      R 1 Reply Last reply
                      0
                      • C Cfer83

                        I think you just gave him the answer to his homework assignment.

                        R Offline
                        R Offline
                        Rocky
                        wrote on last edited by
                        #11

                        so what is that a crime??

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups