By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

programming C+++.i really dun know and confuse

P: 12
i
dun know how to 1. Write a program that uses a do-while loop to print the even numbers between 2 and 60, inclusive, each on a new line.

2. Write a program that asks the user to enter a number between 1 and 100 and uses a do-while loop to error trap the entry of the data. This means that the question will stay inside of the loop until a correct response is entered.

3.Write a program that asks the user to enter his/her test grades (enter -99 to quit). Print the AVERAGE of the test grades entered to the nearest tenth. Use a do-while loop for this program.
please,i really need to know the program listing and the coding,so maybe i'll try to understand it by myself
Sep 18 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
1. Write a program that uses a do-while loop to print the even numbers between 2 and 60, inclusive, each on a new line.
If this is too hard try

1. Write a program that uses a do-while loop to print all the numbers between 1 and 60, inclusive, each on a new line.

You will need to know
  1. How to declare an integer variable, the basic sytanx for variable declaration is

    <type> <name>;

    e.g. int i;
  2. How to increment an integer variable, there are several ways for doing this, these statements all do the same thing, add 1 onto i
    i = i + 1;
    i += 1;
    i++;
    ++i;
  3. How to write a do-while loop, the syntax is
    do {
    /* code statements here */
    while( <endCondition> );

    While the end condition is true the loop keeps looping, when the end condition is false the loop ends, for example

    do {
    /* code statements here */
    while( i<=20 );

    Loops until I has a value greater than 20
  4. How to output the value of an integer to the console, you can use the function printf, it takes a format string that tells it what to output, for an integer

    printf("The value is %d\n", i);

Now have an attempt yourself and post it here
Sep 18 '06 #2

P: 12
i have try like this...thanks ya..now much more better understanding.
#include<iostream>
using namespace std;
int main()
{

int x = 1;
do

{

cout << x << endl;
x += 1;

}

while(x <= 60);
}
Sep 19 '06 #3

P: 12
how about if i change the do-while loop to for loop???lets say if i want to print no from 2 till 60...means the result will be printed out like this...2.4.6.8.10........60...but i have try it like this using "for loop",but why when i use x+=2,the result come out wrongly????..i realised i have to use x+=1...but,why???
thanks for your leisure....
Sep 19 '06 #4

100+
P: 293
D_C
The way you have it now, it starts at 1, and increments by 1, giving 1, 2, 3, ... 60.
If you want 2, 4, 6, ... 60, then you need to start at 2, and increment by 2.

for loop syntax:
Expand|Select|Wrap|Line Numbers
  1. for(INITIALIZE; CONDITION; INCREMENT)
  2. {
  3.   statements...
  4. }
To convert your program as is, it would be:
Expand|Select|Wrap|Line Numbers
  1. for(int x = 1; x <= 60; x+= 1)
  2. {
  3.   cout << x << endl;
  4. }
The above will loop while x is less than or equal to 60, starting with x = 1, incrementing x by one (x += 1) each time. The result will be 1, 2, 3, ... 60.
Sep 19 '06 #5

P: 12
thanks,friend..i understand about that....
Sep 20 '06 #6

Post your reply

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