Thread: Need help with Remainder program

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    7

    Need help with Remainder program

    Hi I'm having difficulty with this remainder program. I'm supposed to make a program that takes 2 numbers and determines which is larger and then divides them. It has to display a remainder, but if a remainder doesn't exist then only the quotient is displayed.
    I got the program to work if I do say 15/15 and no remainder exists but I'm not sure how to program it to not show a remainder if I do 15/3.

    Code:
    #include <iostream>
    #include <cmath>   //included just in case its needed
    using namespace std;
    void main()
    {
        //variables
        int num1;
        int num2;
        int quo;    //quotient
        int rem;        //remainder
    
    
        //prompt and input
        cout<< "Enter a number: ";
        cin>>num1;
    
        cout<<"\n";
    
        cout<< "Enter another number: ";
        cin>>num2;
    
        cout<<"\n";
    
        //calculate
    
    
        quo=num1/num2;
        rem=num1%num2;
    
        if (num1>num2)
    {
        rem=num1%num2;
        quo=num1/num2;
    }
           else if(num2>num1)
           {
           rem=num2%num1;
           quo=num2/num1;
    
        cout<< "The quotient is: "<< quo;
    
        cout<<"\n";
    
        cout<< "With a remainder of: "<<rem;
    
        cout<<"\n";
    }
    
        else if(rem==0)
    {
        quo=num1/num2;
        cout<<" The quotient is: "<< quo;
        cout<<"\n";
        cout<<"There is no remainder.";
    }
    cout<<"\n";
    
        system ("pause");
    }

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    7
    Rewrote the code, it seems to work for remainders except when doing num2>num1. How can I fix this?

    Code:
    #include <iostream>
    #include <cmath>   //included just in case its needed
    using namespace std;
    void main()
    {
        //variables
        int num1;
        int num2;
        int quo;    //quotient
        int rem;        //remainder
    
    
        //prompt and input
        cout<< "Enter a number: ";
        cin>>num1;
    
        cout<<"\n";
    
        cout<< "Enter another number: ";
        cin>>num2;
    
        cout<<"\n";
    
        //calculate
    
        if (num1>num2)
        {
        quo=num1/num2;
        rem=num1%num2;
        }
        else if (num2>num1)
        {
        quo=num2/num1;
        rem=num2%num1;
        }
    
    
        if (rem==0)
        {
            quo=num1/num2;
            cout<<"The quotient is: "<<quo;
            cout<<"\n";
            cout<<"There is no remainder.";
        }
            else if(rem>0)
            {
            quo=num1/num2;
            cout<<"The quotient is: "<<quo;
            cout<<"\n";
            cout<<"With a remainder of: "<<rem;
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    cout<<"\n";
        system ("pause");
    }
    Last edited by Gohmer; 02-09-2015 at 09:37 PM.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    7
    If I try to input num2>num1 it gives me the result quotient = 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find a remainder of a division?
    By keivi in forum C Programming
    Replies: 11
    Last Post: 06-11-2013, 05:52 PM
  2. Change maker program remainder troubles.
    By muffinman8641 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2011, 02:41 PM
  3. manipulating the remainder of a float
    By thestien in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 06:55 AM
  4. how to get remainder of a value
    By seal in forum C Programming
    Replies: 2
    Last Post: 09-22-2005, 07:03 AM
  5. Determining if a number has a remainder?
    By Captain Penguin in forum C Programming
    Replies: 3
    Last Post: 09-01-2001, 07:07 AM