C/Loop Question
Expert: Zlatko - 3/30/2010
QuestionIm
not sure how to actually start the code... The code I have been doing keeps giving the incorrect output....
Write a c++ program containing a loop to print all
the integers from 1 to 100 that have a remainder of
three (3) when divided by seven (7).
I really need some suggestions on to how to do this code please!!!!
Thanks!!!
This is what I have so far:
#include <iostream>
int main()
{
int num;
num = 1;
while (num <= 100)
if ( num % 7 == 3 )
{
cout << num << " ";
}
return 0;
}
AnswerThat's pretty good, but you are missing the increment of the loop counter "num".
Also, you probably need "using namespace std" at the start of your file.
Objects like cout are in the std namespace, so you can refer to them as std::cout, or you can specify that you're using the std namespace for the entire file.
#include <iostream>
using namespace std;
int main()
{
int num;
num = 1;
while (num <= 100)
{
if ( num % 7 == 3 )
{
cout << num << " ";
}
++num; // This was missing
}
return 0;
}