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;
}
}
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:
#include<iostream>
usingnamespace 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;
}
....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.
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.)
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.