### 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.

 ``12345678`` ``````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..

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 ".

Last edited on
yes..
Hi Junkz.

this is what i've done.

floop for..
 ``12345678910111213141516171819202122232425262728293031`` ``````/**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 using namespace std; int main() { int num; int sum = 0; cout << "Please Input Number"<> 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.
 ``1234567891011121314151617181920212223242526`` ``````#include using namespace std; int main() { int num; int sum = 0; cout << "Please Input Number"<> 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
 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; int main() { int num; int sum = 0; cout << "Please Input Number"<> 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; } ``````

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.