Use modulo and conditional to make rows/table?

I have a problem for school (below) I have the first part but I need help with the second part. Also if you could confirm the first part that would be nice. The "i%2" is my attempt at the second part.

"Write a loop that tests all positive three digit numbers to see if the sum of digits is greater than 6. All numbers that pass the test should be outputted in a table format with five numbers per row. Use the modulo operator and a conditional to print the output in rows."
1
2
3
4
5
6
7
8
9
10
 int sum=0;

    for(int i=100; i<=999; i++){
        sum=i/100;
        sum+=i%10/10;
        sum+=i/100%10;
        if(i%2 && sum>6){
            cout<< i <<endl;
        }
        }
Last edited on
The i%2 on line 7 is simply going to test if the number is odd. It's not going to give you 5 numbers per row.

Last edited on
If I am not mistaken, the modulo operator is used to find the remainder....?
No you are not mistaken. But that's what the assignment is asking. I'm sure I'm doing something wrong.
I just looked at your code and compiled it in Visual Studio. Here is what I added to make it compile. The output only displays odd numbers starting at 403. I had to add a couple brackets and of course the main to make it compile but here is what I had:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

using namespace std;

int main()
{
	int sum = 0;

	for (int i = 100; i <= 999; i++)
        {
		sum = i / 100;
		sum += i % 10 / 10;
		sum += i / 100 % 10;
		if (i % 2 && sum > 6)
                {
			cout << i << endl;
		}
	}
	return 0;
}
Well yeah that's what I have in codeblocks...I didn't add it because I just need help with the main code. Thanks though...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;


int main()
{

for(int i=100, j, sum, k=0; i<502;i++) //502 to 999 all qualified
  {
	sum =0; // reset every iteration
	j=i;
	
	sum=j %10; j = j/10;
	sum+=j %10; j = j/10;
	sum+=j %10; j = j/10;
	
	if(sum>6)
	 {  
	   cout << i << " ";
	   k++;
	   if(k%5==0)
	 	{
	 	  k=0;
	 	  cout << "\n";		 			
	 	}
	 }
  }
	
return 0;
}
....All numbers that pass the test should be outputted in a table format with 
five numbers per row. Use the modulo operator and a conditional to print the output
 in rows.


The way I'd usually do this is to keep a count of the number of items I've printed out. Once I've reached the number of items on one row that I want, I output a new line.
Last edited on
hey there wildblue. Listen to this guy, he has help me a bunch in the past. As a matter of fact, if you are familiar with polymorphism, could you take a look at my basic code I posted in the General section when you get a chance? (Not trying to steal him, I just know he is very knowledgeable.)
Last edited on
Well actually I figured it out from what anup30 said. But I'm sure wildblue is very knowledgeable :P.
for(int i=100, j, sum, k=0; i<502;i++) //502 to 999 all qualified why start at 502? The assignment is to output everything that the sum of the 3 numbers is greater than 6.
I think he did it to short the problem. We know everything between 502 and 999 are greater than 6 so it's not important.
Hmm. Well this is what I have as of now. But for some reason it outputs everything greater than 400... Suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int sum=0;
    int r=0;

    for(int i=100; i<=999; i++){
        sum=i/100;
        sum+=i%10/10;
        sum+=i/100%10;

            if(sum>6){
                cout<< i <<" ";
                    r++;
                    if(r%5==0){
                    r=0;
                    cout<<"\n";}
        }
        }
    return 0;
}
Got it. Makes sense.
@and kand 97,
 
 sum=i/100 + (i%100)/10 + (i%100)%10;
Topic archived. No new replies allowed.