Loop Question

Hewlett-packard 4x dvd module mpeg decod...
March 29, 2010 at 20:44:22
Specs: Windows 7
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!1


See More: Loop Question

Report •

#1
March 29, 2010 at 20:55:16
I'm sure you know how to programme a loop.

In order to do the mathematics you need to be looking at the Modulus function.

Google will give you plenty of examples of how to use the Modulus function.

Stuart


Report •

#2
March 29, 2010 at 21:07:30
I have this so far:

#include <iostream>

int main()
{
int num;
num = 1;
while (num <= 100)

cin >> num;

if ( num % 7 == 3 )
{
cout << num << " ";
}

return 0;
}


Report •

#3
March 29, 2010 at 22:06:53
modulo will work, but simple addition also will serve:
#include <iostream.h>
int main() {
int i;
int j;
for (i=0;i<100;i+=7) {
j=i+3;
cout<<j<<" ";
}
return 0;
}
/*homework solutions aside, I'm also struggling with C, so i had to take a swing at it. the simplest things are constantly confounding me! this lets me work on a problem and learn.*/

Report •

Related Solutions

#4
March 29, 2010 at 22:46:47
@nbrane

It could be simplified further:

#include <iostream>
int main() {
    for (int i=3;i<101;i+=7) {
        std::cout << i << " ";
    }
    return 0;
}

Also <iostream.h> is depreciated in c++, instead <iostream> is the recommended header.


Report •

#5
March 29, 2010 at 22:52:20
cee? that's exactly what i mean Lol!! good point about the logic, though, duh me! i have no clue which compiler i have... I remain clueless as to which thingeys work... I guess i should try to figure these things out. ;)

Report •

#6
March 29, 2010 at 23:28:35
@nbrane

I am in no way and expert on C/C++, I really consider myself an "advanced beginner". It can be quite difficult at first, especially when it comes to pointers and other memory related tasks(less so for C++).

Really they just hasn't got the extra layer of abstraction most newer languages have, but you can create your own or use libraries.

Hang in long enough and I will get a bit easier.

BTW what are you using(possibly an IDE)?


Report •

Ask Question