Looping Drills

Write your question here.
Hi im just starting out c++ and my teacher is not helpful at all and i really want to learn. The issue i am having is im trying add all the numbers that that were not skipped and add all the even and odds that are left over, so fat i managed to print the numbers i did not skip.


11. add up all the numbers from min to max, skipping multiples of ETC, and show the total odd numbers, total even numbers, and the grand total, average odd, average even, and average of all the numbers.

1 2 3 x 5 6 7 x 9 = 33
2 6 = 8
1 3 5 7 9 = 25
---- Manipulating TWO variables in a loop: -----



Imagine the numbers between min and max in a row...

in another row, imagine odd numbers between min and max

min: 1

max: 10

1 2 3 4 5 6 7 8 9
1 3 5 7 9

#include <iostream>

using namespace std;

int main()
{
int max = 0;
int min = 1;
int number1 = 0;
int number2 = 0;
int etc;
int odd;
int even;
cout<<"Min number is : "<< min <<endl;


cout<<"Enter max number: ";
cin>>number2;
cout<<endl<<endl;

{
if(number1>number2){
min=number1;
max=number2;
}else{
min=number1;
max=number2;

}
// problem 10
}
int number0;
long odd_sum=0,even_sum=0;


for(number0 = min; number0 <= max; number0++)
if(number0 %2 != 0)
odd_sum = odd_sum + number0;
else
even_sum = even_sum + number0;


cout << "The sum of even numbers is: "<< even_sum << endl;
cout << "The sum of odd numbers is: "<< odd_sum <<endl;
cout << "The grand total is: "<< odd_sum + even_sum << endl;







cout<<"Enter a multiple to skip: ";
cin>>etc;

for(number0 = min; number0 <= max; number0++)
{
if(number0 %etc == 0)
{}
else
cout<< number0 <<", ";


}





return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for ( number0 = min; number0 <= max; ++number0 )
{
  if ( number0 %etc == 0 )
  {
    // skipped multiple
  }
  else
  {
    // not skipped
    cout<< number0 <<", ";
    if ( ?? )
    { // odd
    }
    else
    { // even
    }
  }
}
// calculate averages. Averages need count of items too.   


The title of your post is "looping drills" but I'm not seeing any loops in your code. I think you need FOR loops.

for example, a for loop declares the starting value, the loop until condition, and the increment or sentinal value to end the loop.

1
2
3
4
total=0;
for (int x=0; x<10;x++){  //this will loop 10 times, from 0 to 9.  It stops when x=10.
  total+=x; //this will add the value of x to total (0+0, 0+1, 1+2, 3+3, 4+6) and so one
}
Last edited on
Topic archived. No new replies allowed.