Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Requires: n > 0
- //Modifies: nothing
- //Effects: prints a series of values
- // if n is even, halve it
- // if n is odd, triple it and add one
- // continue until n is 1
- // starting with 3 gives
- // 3, 10, 5, 16, 8, 4, 2, 1
- void threeSeries (int n)
- {
- if(n<=0) //Returns if number is 0 or negative
- return;
- if( n > 0)
- {
- cout<<n<<", ";
- while(n != 1) //Declaring the condition for this loop (while n isn't = to 1)
- {
- if(n%2 == 0) //This statement finds even numbers
- {
- n /= 2; //Halves n
- cout<<n<<", ";
- }
- else if(n%2 == 1) //This statement finds odd numbers
- {
- n = (n*3) +1; //Triples n and adds 1
- cout<<n<<", ";
- }
- }
- cout<<endl;
- }
- }
- to:
- //Requires: n > 0
- //Modifies: nothing
- //Effects: prints a series of values
- // if n is even, halve it
- // if n is odd, triple it and add one
- // continue until n is 1
- // starting with 3 gives
- // 3, 10, 5, 16, 8, 4, 2, 1
- void threeSeries (int n)
- {
- if(n<=0) //Returns if number is 0 or negative
- return;
- if( n > 0)
- {
- cout<<n<<", ";
- while(n > 1) //Declaring the condition for this loop (while n isn't = to 1)
- {
- if(n%2 == 0) //This statement finds even numbers
- {
- n /= 2; //Halves n
- cout<<n<<", ";
- }
- else if(n%2 == 1) //This statement finds odd numbers
- {
- n = (n*3) +1; //Triples n and adds 1
- cout<<n<<", ";
- }
- }
- cout<<"1"<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement