| | |
Problem with program, integer divisble by 7 and 2, also average question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2007
Posts: 21
Reputation:
Solved Threads: 0
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.
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.
c Syntax (Toggle Plain Text)
#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:
c Syntax (Toggle Plain Text)
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2007
Posts: 21
Reputation:
Solved Threads: 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.
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.
•
•
•
•
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.
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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
That's what the IF I posted does.••••Originally Posted by Rickenbacker360Also I should use and since I'm checking that the integer I entered was divisible by both 2 and 7, correct?
Last edited by psycotic_furby; Feb 15th, 2007 at 9:33 pm.
if(bagType==paper) {break;)
•
•
•
•
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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
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.
Should work, as it's shamelessly stolen... sorry, adapted from WaltP's version...
c++ Syntax (Toggle Plain Text)
if( ((n % 2) == 0) && ((n % 7) == 0) )
Should work, as it's shamelessly stolen... sorry, adapted from WaltP's version...
if(bagType==paper) {break;)
![]() |
Other Threads in the C++ Forum
- Previous Thread: problem with opening 2 or more files in c++
- Next Thread: FindNextFile() problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






