One: As presented, your code has no formatting. If it originally had any formatting, then that has been stripped away since everything here is in HTML -- leading spaces disappear. You need to present your code using code tags
using namespace std;
int num ;
if ( (num % 2 == 1) && (num % 3 == 1) && (num % 4 == 1) && (num % 5 == 1) && (num % 6 == 1) && (num % 7 == 0) )
cout << " The number of students is " << num << endl ;
return 0 ;
Learn it! Use it!
Two: The logic of your if-statement is guaranteed to yield a false.
Please present me with one single number that is evenly divisible by 7 and yet has a remainder of 1 when divided by 2, 3, 4, 5, and 6. That is what you are testing for and, I believe, you are guaranteed to never satisfy the conditions of that if-statement. Maybe such a number exists, but it would be very esoteric indeed, and extremely rare.
Oops! Excuse me! Your assignment is to come up with such a number! Carry on!
Three: num has no value! Or rather, it contains a single value, a garbage value
, since that local variable is uninitialized and hence contains garbage
. What is the probability that some random garbage
value is magically the one you are seeking? Mindbogglingly nil!
OK, now stop and think for a change. You are to employ a "brute force" approach. That means that you try every possibility. OK, what that tells me is that you try each and every possible value; that is the very definition of "brute force". Which suggests what? That you try multiple values. What values? Every single positive value the num can have.
Now what does that
suggest? A for-loop, right? Use the for-loop to assign a value to num and then test num for satisfying the conditions, right?
So why don't you use a for-loop to go from 7 to INT_MAX (consult and #include your compiler's limits.h header file)? That would be the brute-force approach. Though as an optimization, you could iterate by 7 rather than by 1. After all, each number needs to be a multiple of 7, right?
Think about that for a bit.