Mira2706

hw1_4

Nov 18th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. //task 4
  7. int main()
  8. {
  9.     int n=1;
  10.     cout<<"Beginning of the sequence: ";
  11.     cin>>n;
  12.     int counter = 0;
  13.  
  14.     if (n<0){
  15.         cout<<"Invalid input."<<endl;
  16.     }
  17.  
  18.     for(int i = n; n > 1; i--)
  19.     {
  20.         if(n%2==0)
  21.         {
  22.             n/=2;
  23.         }
  24.  
  25.         else if(n%2==1)
  26.         {
  27.             n=3*n+1;
  28.         }
  29.  
  30.  
  31.         cout<<n<<endl;
  32.         counter++;
  33.     }
  34.     cout<<"elements of the sequence: "<<counter + 1<<endl;
  35.     cout<<"The number 999 999 has the most elements in its sequence"<<endl;
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment