Identify the digits in a given number.

前端 未结 7 734
滥情空心
滥情空心 2020-12-07 03:41

I\'m new to programming, and I\'m stuck at a problem. I want my program to identify the separate digits in a given number, like if I input 4692, it should ident

7条回答
  •  渐次进展
    2020-12-07 04:03

    Another approach is to have two loops.

    1) First loop: Reverse the number.

    int j = 0;
    while( i ) {
       j *= 10;
       j += i % 10;
       i /= 10;
    }
    

    2) Second loop: Print the numbers from right to left.

    while( j ) {
       std::cout << j % 10 << ' ';
       j /= 10;
    }
    

    This is assuming you want the digits printed from right to left. I noticed there are several solutions here that do not have this assumption. If not, then just the second loop would suffice.

提交回复
热议问题