Search code examples
c++countdown

How to countdown to 0 C++


I need to count down to 0. I am only printing 0 to the screen. How can I print all the count-down characters to the screen? Below is the code I am using right now.

#include <stdio.h>
#include <iostream>
using namespace std;

class Solution {
public:
    int num;
    int numberOfSteps (int num) 
    {
      while (num != 0)
      {
        if (num % 2 == 0)
          {
            num = num / 2;
            cout << num;
          }
        else
        {
            num = num - 1;
            cout << num;
        }
    }

    }
};
int main () {
    int num;
    Solution myObj;
    cin >> num;
    cout << myObj.num;
    }

Solution

  • I am revisiting this question and have created a simpler solution than my original post:

        #include <iostream>
    
        using namespace std;
    
        int num;
    
        int main()
        {
        cout << "Please enter the number you would like to count down to zero : ";
        cin >> num;
    
            while (num > 0)
            {
                cout << num << endl; 
                num--;
            }
    
        cout << "The number is now zero.";
        return 0;
        }