i need to write a basic c++ cmd program that uses the while loop to display the odd numbers and sum of the even numbers in between 2 numbers that the users types in. Im not sure how to go about this. if someone could show me how to get it to list of all numbers inbetween the numbers i think i could figure it out from there
while loop question
Collapse
X
-
You could initialize a variable to the starting number, then have that variable increase by 1 in your while loop. Make your while loop stop when the variable reaches the ending value. -
can you give me an example im kinda new to this not really sure how i made it through the first class but i did somehowComment
-
Lets say the user nominates start at 23 and finish at 59. You could print out each pair of numbers (integers) between 23 and 59 (inclusive) with a for loop.
You could assign these pairs of numbers to a C++ string or to an array or vector with:Code:e.g for(i=start;i<finish;i++) cout<<i;
Now print the numbers using an identical for loop (or the first one) usingCode:array a [i]=i; or v.push_back i; or string s+=i; //as part of above loop.
Do you get the idea?Code:if(i%2==0)sum_even+=i;/* and print the odd numbers with*/ if(i%2!=0) cout<<i
Comment
-
Thanks man I appreciate it alot. Hopefully soon I won't have get online to figure out basic shit like this.Lets say the user nominates start at 23 and finish at 59. You could print out each pair of numbers (integers) between 23 and 59 (inclusive) with a for loop.
You could assign these pairs of numbers to a C++ string or to an array or vector with:Code:e.g for(i=start;i<finish;i++) cout<<i;
Now print the numbers using an identical for loop (or the first one) usingCode:array a [i]=i; or v.push_back i; or string s+=i; //as part of above loop.
Do you get the idea?Code:if(i%2==0)sum_even+=i;/* and print the odd numbers with*/ if(i%2!=0) cout<<i
Comment
Comment