944,106 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2166
  • C++ RSS
Feb 14th, 2007
0

Problem with program, integer divisble by 7 and 2, also average question

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rickenbacker360 is offline Offline
21 posts
since Jan 2007
Feb 14th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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 [code][/code] around your code? The post was kinda strange and I'm wondering if you did something different?
  1.  
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. int n;
  9.  
  10. cout << "Please enter an integer: " << endl;
  11. cin>>n;
  12.  
  13. for( int i = 0; i <= n; i++ ) //// you need braces around all the code
  14. //// you want in the FOR loop.
  15. cout << i << endl;
  16.  
  17. if( n/2 && n/7 ) ////This will rarely work. See below
  18. //// Use braces here, also....
  19. cout << "The integer you entered is divisible by 2 and 7" <<endl;
  20.  
  21. else
  22.  
  23. cout << "The integer you entered is not divisible" << endl;
  24.  
  25. return 0;
  26. }

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:
  1. if( ((n % 2) == 0) || ((n % 7) == 0) )
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Feb 14th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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.
Last edited by Rickenbacker360; Feb 14th, 2007 at 3:07 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rickenbacker360 is offline Offline
21 posts
since Jan 2007
Feb 14th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Feb 15th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

Click to Expand / Collapse  Quote originally posted by WaltP ...
Quote originally posted by Rickenbacker360 ...
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.
Last edited by psycotic_furby; Feb 15th, 2007 at 9:33 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psycotic_furby is offline Offline
3 posts
since Feb 2007
Feb 15th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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..
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 16th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

...can be divided by both 2 and 7
Ahhh, so I did misread. Sorry.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Feb 16th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nadith_cs is offline Offline
13 posts
since Feb 2007
Feb 16th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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";
Reputation Points: 11
Solved Threads: 0
Newbie Poster
nadith_cs is offline Offline
13 posts
since Feb 2007
Feb 16th, 2007
0

Re: Problem with program, integer divisble by 7 and 2, also average question

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.

c++ Syntax (Toggle Plain Text)
  1. if( ((n % 2) == 0) && ((n % 7) == 0) )

Should work, as it's shamelessly stolen... sorry, adapted from WaltP's version...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
psycotic_furby is offline Offline
3 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem with opening 2 or more files in c++
Next Thread in C++ Forum Timeline: FindNextFile() problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC