Dividing numbers?

This is a discussion on Dividing numbers? within the C++ Programming forums, part of the General Programming Boards category; How do you divide numbers in c++?...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    Dividing numbers?

    How do you divide numbers in c++?

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    /

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    If the explanation above isn't comprehensive enough have a look at this.

    Code:
    #include <iostream.h>
    
    int main()
    {
    	 int num1, num2, answer;
        
    	 cout<<"Please enter number 1: ";
    	 cin>>num1;
    
    	 cout<<"Please enter number 2: ";
        cin>>num2;
    
    	 answer = num1 / num2;
    
    	 cout<<num1<<" divided by "<<num2<<" = "<<answer;
    
    return 0;
    }
    hope that helps.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    But remember that when you're working with integers, everything will be rounded down. That means that 12/7=1.

  5. #5
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    To get the remainder, use modulus, %.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    To get a more exact answer, use floats:

    float Nr1 = 45.0;
    float Nr2 = 21.0;

    float Answer = Nr1 / Nr2; //Answer = 2,1428...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. Replies: 4
    Last Post: 03-03-2003, 03:52 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Dividing int numbers by 256, but fast!
    By Boksha in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 03:44 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21