while loop question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shane3673
    New Member
    • Apr 2010
    • 3

    #1

    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
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

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

    Comment

    • shane3673
      New Member
      • Apr 2010
      • 3

      #3
      Originally posted by YarrOfDoom
      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 somehow

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

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

        Code:
        e.g for(i=start;i<finish;i++)
               cout<<i;
        You could assign these pairs of numbers to a C++ string or to an array or vector with:
        Code:
        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
        Code:
        if(i%2==0)sum_even+=i;/* and print the odd numbers with*/ if(i%2!=0) cout<<i
        Do you get the idea?

        Comment

        • shane3673
          New Member
          • Apr 2010
          • 3

          #5
          Originally posted by whodgson
          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.

          Code:
          e.g for(i=start;i<finish;i++)
                 cout<<i;
          You could assign these pairs of numbers to a C++ string or to an array or vector with:
          Code:
          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
          Code:
          if(i%2==0)sum_even+=i;/* and print the odd numbers with*/ if(i%2!=0) cout<<i
          Do you get the idea?
          Thanks man I appreciate it alot. Hopefully soon I won't have get online to figure out basic shit like this.

          Comment

          Working...