How do I split an int into its digits?

前端 未结 15 1565
渐次进展
渐次进展 2020-11-27 17:51

How can I split an int in c++ to its single numbers? For example, I\'d like to split 23 to 2 and 3.

15条回答
  •  醉酒成梦
    2020-11-27 18:17

    A simple answer to this question can be:

    1. Read A Number "n" From The User.
    2. Using While Loop Make Sure Its Not Zero.
    3. Take modulus 10 Of The Number "n"..This Will Give You Its Last Digit.
    4. Then Divide The Number "n" By 10..This Removes The Last Digit of Number "n" since in int decimal part is omitted.
    5. Display Out The Number.

    I Think It Will Help. I Used Simple Code Like:

    #include 
    using namespace std;
    int main()
    {int n,r;
    
        cout<<"Enter Your Number:";
        cin>>n;
    
    
        while(n!=0)
        {
                   r=n%10;
                   n=n/10;
    
                   cout<

提交回复
热议问题