Split an Integer into its digits c++

后端 未结 12 1899
说谎
说谎 2020-11-28 15:27

I\'m trying to learn c++ on my own and I\'ve hit a bit of a road block. The problem is I need to take an integer,split it into its digits and get the sum of the digits and

12条回答
  •  广开言路
    2020-11-28 16:11

    I used this for input for 5 numbers

    int main () {

    using namespace std;

    int number;`

    cout << "Enter your digit: ";
    cin >> number;
    if (number == 0)
      return 0;
    
    int a = number % 10;
    int second = (number / 10);
    
    int b = second % 10;
    int third = (second / 10);
    
    int c = third % 10;
    int fourth = (third / 10);
    
    int d = fourth % 10;
    int fifth = (fourth / 10);
    
    int e = fifth % 10;
    
    cout << e << " " << d << " " << c << " " << b << " " << a << endl;
    
    return 0;
    

    }

提交回复
热议问题