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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nor farhana yaakub
    New Member
    • Sep 2006
    • 12

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

    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

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

    Comment

    • Nor farhana yaakub
      New Member
      • Sep 2006
      • 12

      #3
      i have try like this...thanks ya..now much more better understanding.
      #include<iostre am>
      using namespace std;
      int main()
      {

      int x = 1;
      do

      {

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

      }

      while(x <= 60);
      }

      Comment

      • Nor farhana yaakub
        New Member
        • Sep 2006
        • 12

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

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          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:
          Code:
          for(INITIALIZE; CONDITION; INCREMENT)
          {
            statements...
          }
          To convert your program as is, it would be:
          Code:
          for(int x = 1; x <= 60; x+= 1)
          {
            cout << x << endl;
          }
          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.

          Comment

          • Nor farhana yaakub
            New Member
            • Sep 2006
            • 12

            #6
            thanks,friend.. i understand about that....

            Comment

            Working...