You are here:

C/Loop Question

Advertisement


Question
Im
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;
}

Answer
That'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;
}

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.