Hey again, I was hoping someone here could look at my code and tell me why I'm not receiving the correct output.

Here's what I'm supposed to do:Write a program that asks a user to enter an integer, say n,then list all the numbers that are less than or equal to n and can be divided by both 2 and 7

#include<iostream>
   using namespace std;
 
   int main()
   {
 
   int n;
 
   cout << "Please enter an integer: " << endl;
   cin>>n;
 
   for( int i = 0; i <=  n; i++ ) 
   cout << i << endl;
 
   if( n/2 && n/7 )
 
       cout << "The integer you entered is divisible by 2 and 7" <<endl;
 
   else
 
       cout << "The integer you entered is not divisible" << endl;
 
   return 0;
}

When I run the program it will show all the numbers less than the integer I entered. However when it comes to finding out if the number is divisible by 2 and 7, no matter what number I enter it says it's divisible by 2 and 7.

If someone could show me what I'm doing wrong i'd appreciate it.

Hey again, I was hoping someone here could look at my code and tell me why I'm not receiving the correct output.

Here's what I'm supposed to do:Write a program that asks a user to enter an integer, say n,then list all the numbers that are less than or equal to n and can be divided by both 2 and 7

When I run the program it will show all the numbers less than the integer I entered. However when it comes to finding out if the number is divisible by 2 and 7, no matter what number I enter it says it's divisible by 2 and 7.

If someone could show me what I'm doing wrong i'd appreciate it.

Sure. First I have a question. Did you do something other than type around your code? The post was kinda strange and I'm wondering if you did something different?

#include<iostream>
using namespace std;

int main()
{

   int n;

   cout << "Please enter an integer: " << endl;
   cin>>n;

   for( int i = 0; i <=  n; i++ ) //// you need braces around all the code
                                  //// you want in the FOR loop.
   cout << i << endl;

   if( n/2 && n/7 )    ////This will rarely work.  See below
                       //// Use braces here, also.... 
       cout << "The integer you entered is divisible by 2 and 7" <<endl;

   else

       cout << "The integer you entered is not divisible" << endl;

   return 0;
}

The fix:
1) If you want to test for one of two possible values, you need an OR, not AND
2) If you want to test for divisibility, you need the modulus (%) operator
3) Since you are using math you should use parentheses and equality for best understanding:

if( ((n % 2) == 0) || ((n % 7) == 0) )

thanks for the reply, seems like you're always helping me out. what do you mean when you say that my post was kind of strange? What I did was copied my code from the compiler then pasted it. The only thing I did was space things out a bit, because you told me once before that my formatting looked bad and that I should space things out.

Also I should use and since I'm checking that the integer I entered was divisible by both 2 and 7, correct?

Thanks again for the help.

thanks for the reply, seems like you're always helping me out. what do you mean when you say that my post was kind of strange? What I did was copied my code from the compiler then pasted it. The only thing I did was space things out a bit, because you told me once before that my formatting looked bad and that I should space things out.

Interesting. Your post had a color tags on every line of the code. I wonder if your IDE copies the color.... Nyah, that doesn't make sense...

Your spacing is good. Your intenting needs a little work, and make more use of {} and () ;)

Also I should use and since I'm checking that the integer I entered was divisible by both 2 and 7, correct?

That's what the IF I posted does.

Also I should use and since I'm checking that the integer I entered was divisible by both 2 and 7, correct?

That's what the IF I posted does.

Correct me if I'm wrong (first post, it's quite likely!) but I think this does need to be an and - he want's to know if the number is divisible by seven AND two, not one or the other.

Correct me if I'm wrong (first post, it's quite likely!) but I think this does need to be an and - he want's to know if the number is divisible by seven AND two, not one or the other.

Yes, it does look like WaltP slightly misread the code...

However, the issue at hand here was more whether to use modulus (%) or division operator. The point that WaltP was making was that you can't determine whether a number goes into another one evenly with the division operator. You have to check that the remainder of the division is 0, and so you use the modulus operator to return the remainder..

...can be divided by both 2 and 7

Ahhh, so I did misread. Sorry.

ok i got ur problem;
i suppose u know integer division well;
when u say n/2&&n/7 and when u enter a number for n between -7<n<7 u will get the correct message.
but when u enter a number outside of tht range
eg: n=15
n/2=7
n/7=2
therefore (n/2&&n/7) is true

ok as a solution i suggest a simple maths theorm....
if a number is divisable by both 2 and 7 the common factor is 14...
which means tht num should be divisable by 14.... therefore in ur condition state as follows......
if (!(n/14.0-n/14)) cout<<"divisable by 7 and 2";

nadith, WaltP's solution using the modulus operator was correct, except for using OR instead of AND. Dividing two numbers doesn't test them for anything.

if( ((n % 2) == 0) && ((n % 7) == 0) )

Should work, as it's shamelessly stolen... sorry, adapted from WaltP's version... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.