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.
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...
/**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>
usingnamespace 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;
}