-2

I am learning c++ and have been set a task of finding where an inputted number is divisible by 5 or 6 or both using the && and || operators. I am really just beginning to learn c++ and so don't really even know how to approach this.

  • Have you written C++ programs before? If not start with some introductory text (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and not with this particular problem. If you have already written C++ programs, then what exactly makes it difficult for you to find a start here? –  Nov 17 '18 at 18:15
  • 1
    What is your specific question? – Thomas Sablik Nov 17 '18 at 18:25
  • sorry for being unclear, I was wondering what && and || operators would look like in use. I have coded in python in the past, and so I tried to implement it as I would in python but can't get it to work. so I thought some example of how they would be implemented in c++ would be helpful. thanks. – user10667609 Nov 18 '18 at 01:03

1 Answers1

0

You will want to use the modulus to check if a number is evenly divisible by another. The modulus check to see if there is anything left over after division. Here is a simple program using all the requirements that you specified.

#include <iostream>
using namespace std;

int main()
{
    int in;
    cout << "Please enter a number and I wil check if it is divisible by 5 or 6: ";
    cin >> in;
    cout << endl;

    if(in %5 == 0 && in %6 == 0)
        cout << "Number is divisible by both 5 and 6" << endl;
    if(in %5 == 0 || in %6 == 0)
    {
        if(in %5 == 0)
            cout << "Number is divisible by 5 and not 6" << endl;
        if(in %6 == 0)
            cout << "Number is divisible by 6 and not 5" << endl;
    }
    else
        cout << "Number is not divisible by 5 or 6" << endl;

    return 0;
}
idzireit
  • 98
  • 1
  • 7