pls Make me understand with this question.

i am starting to study c++. just know only basic c++.
this is the question i need to understand how to do..
for time being,, my lecturerr didnt teach me about how to use loops.
iam still study with online tutorial.. :(



By using the looping control structure.
by using a different type of loop
1. for
2. while and
3. do…while)

Ask the user for a number.
Sum up the odd numbers that are less than or equal to the user entered number.
Print out the sum.

Example.

1
2
3
4
5
6
7
8
Input    Calculation    Sum
   0                     0
   1          1          1
   2          1          1
   3        1 + 3        4
   4        1 + 3        4
   5      1 + 3 + 5      9
   6      1 + 3 + 5      9


and so on..

your help is much appreciate.

Last edited on
try this source code (loop for)
maybe you'll understand when you see this code
good luck...!!

#include <iostream>

using namespace std;

int main()
{
int loop,mod,sum;
sum=0;

cout<<"enter number : ";
cin>>loop;
cout<<"loop sum"<<endl;

for(int i=0;i<loop;i++)
{
mod=i%2;

if(mod!=0)
{
sum+=i;
cout<<i<<" "<<sum<<endl;
}
else
{
cout<<i<<" "<<sum<<endl;
}
}

return 0;
}
sorry mr junkz

is this the right answer for the question by using the loop " for "?
( edited - already try and the answer is right - please correct me if iam wrong )
if yes ..i will try to understand loop "while " and loop " do ... while ".

tq so much,, but still need time to understand by reading reading and reading...

Last edited on
yes..
Hi Junkz.

this is what i've done.

floop for..
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
31
/**PART A - LOOP FOR
 *   1: C++ programs, type of loop FOR to solve the following problem.
 *   2: Ask the user for a number.
 *   3: Sum up the odd numbers that are less than or equal to the user entered number.
 *   4: Print out the sum.
*/
#include <iostream>
using namespace std;
int main()
{
    int num;
    int sum = 0;
    cout << "Please Input Number"<<endl;
    cin >> num;
    int n;

/** Process */
    cout << "loop    " << "sum" << endl;

    for (n = 0; n < num; n++){
        if(n % 2 == 1){
            int  sum1 =  n;
            sum = sum + sum1;
            cout << n << "       "<< sum << endl;
        }
        else {
             cout << n << "       "<< sum << endl;
        }}
    return 0;
}


===============================================\

While.
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
#include <iostream>
using namespace std;
int main()
{
    int num;
    int sum = 0;
    cout << "Please Input Number"<<endl;
    cin >> num;
    int n;

/** Process */
    cout << "loop    " << "sum" << endl;
    n = 0;
    do {
        if(n % 2 == 1){
          int  sum1 =  n;
          sum = sum + sum1;
           cout << n << "       "<< sum << endl;
        }
        else {
             cout << n << "       "<< sum << endl;
        }
        ++n;
    }while (n < num);
    return 0;
}


=============================================\

do.. while
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
#include <iostream>
using namespace std;
int main()
{
    int num;
    int sum = 0;
    cout << "Please Input Number"<<endl;
    cin >> num;
    int n;
/** Process */
    cout << "loop    " << "sum" << endl;

    n = 0;
    while (n < num) {
        if(n % 2 == 1){
            int  sum1 =  n;
            sum = sum + sum1;
            cout << n << "       "<< sum << endl;
        }
        else {
             cout << n << "       "<< sum << endl;
        }
        ++n;
    }
    return 0;
}



please comment. Tq
good job..

but i think ,, no need " sum1 "

if (n % 2 == 1)
{
//int sum1 = n; unnecessary variable
sum = sum + n;
cout << n << " "<< sum << endl;
}
Topic archived. No new replies allowed.