C/C++ Programming Learning-Week 6 ⑦ Separate each digit of an integer

Topic link

Title description

Xiao Suan gave you an integer and asked to separate each digit from the ones digit.

Input format
enter an integer, whole number from 1 to 10 . 8 between.

Output format
Starting from the ones place, output each digit in sequence from low to high. The numbers are separated by a space.

Sample Input

123

Sample Output

3 2 1

Ideas

Use% 10 and /10 to separate the parts of the integer.

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n;
	while(cin >> n)
	{
    
    
		while(n)
		{
    
    
			if(n < 10) cout << n % 10;
			else cout << n % 10 << " ";
			n /= 10;
		}
		cout << endl;
	}
	return 0;
}

For students who don’t have C language foundation, you can learn the C language grammar first. I will sort it out and send it out later.
I have already written it. You can go to the C language programming column to see the content of the first week .

Other exercises this week:

C language programming column

C/C++ Programming Learning-Week 6① Calculate A+B (Novice Course)

C/C++ programming learning-week 6② A*B problem

C/C++ Programming Learning-Week 6 ③ Class size

C/C++ Programming Learning-Week 6 ④ Sum of odd numbers

C/C++ Programming Learning-Week 6 ⑤ Calculation of the bounce height of the ball

C/C++ Programming Learning-Week 6 ⑥ Image similarity

C/C++ Programming Learning-Week 6 ⑦ Separate each digit of an integer

C/C++ Programming Learning-Week 6⑧ Simple Calculator

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/112911945