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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 21
Reputation: Rickenbacker360 is an unknown quantity at this point 
Solved Threads: 0
Rickenbacker360 Rickenbacker360 is offline Offline
Newbie Poster

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

 
0
  #1
Feb 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #2
Feb 14th, 2007
Originally Posted by Rickenbacker360 View 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

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) )
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: Rickenbacker360 is an unknown quantity at this point 
Solved Threads: 0
Rickenbacker360 Rickenbacker360 is offline Offline
Newbie Poster

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

 
0
  #3
Feb 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #4
Feb 14th, 2007
Originally Posted by Rickenbacker360 View Post
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 ()

Originally Posted by Rickenbacker360 View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: psycotic_furby is an unknown quantity at this point 
Solved Threads: 0
psycotic_furby's Avatar
psycotic_furby psycotic_furby is offline Offline
Newbie Poster

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

 
0
  #5
Feb 15th, 2007
Originally Posted by WaltP View Post
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.
if(bagType==paper) {break;)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #6
Feb 15th, 2007
Originally Posted by psycotic_furby View Post
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..
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #7
Feb 16th, 2007
Originally Posted by Rickenbacker360 View Post
...can be divided by both 2 and 7
Ahhh, so I did misread. Sorry.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 13
Reputation: nadith_cs is an unknown quantity at this point 
Solved Threads: 0
nadith_cs nadith_cs is offline Offline
Newbie Poster

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

 
0
  #8
Feb 16th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 13
Reputation: nadith_cs is an unknown quantity at this point 
Solved Threads: 0
nadith_cs nadith_cs is offline Offline
Newbie Poster

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

 
0
  #9
Feb 16th, 2007
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";
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: psycotic_furby is an unknown quantity at this point 
Solved Threads: 0
psycotic_furby's Avatar
psycotic_furby psycotic_furby is offline Offline
Newbie Poster

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

 
0
  #10
Feb 16th, 2007
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.

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

Should work, as it's shamelessly stolen... sorry, adapted from WaltP's version...
if(bagType==paper) {break;)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC