473,395 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

while loop question

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
Apr 11 '10 #1
4 2220
YarrOfDoom
1,247 Expert 1GB
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.
Apr 12 '10 #2
@YarrOfDoom
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 somehow
Apr 12 '10 #3
whodgson
542 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. e.g for(i=start;i<finish;i++)
  2.        cout<<i;
You could assign these pairs of numbers to a C++ string or to an array or vector with:
Expand|Select|Wrap|Line Numbers
  1. array a [i]=i; or v.push_back i; or string s+=i; //as part of above loop.
Now print the numbers using an identical for loop (or the first one) using
Expand|Select|Wrap|Line Numbers
  1. if(i%2==0)sum_even+=i;/* and print the odd numbers with*/ if(i%2!=0) cout<<i
Do you get the idea?
Apr 12 '10 #4
@whodgson
Thanks man I appreciate it alot. Hopefully soon I won't have get online to figure out basic shit like this.
Apr 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
8
by: jvb | last post by:
Hey all, I figure it's Wednesday, why not put a question up for debate. Beyond personal preference, is there any benefit (performance or otherwise) to using one loop over the other? For example,...
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
4
by: Uday Bidkar | last post by:
I am trying to register my interface with IConnectionPoint of outlook reminders to capture some Outlook Reminder events and having some issues. Here goes the pseudo code int APIENTRY...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
1
by: somenath | last post by:
Hi All, I have doubt regarding how compiler understands about while loop. For example the bellow mentioned code produce the output as mentioned bellow. #include<stdio.h> int main(void) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.