Can someone explain this to me?

Write your question here.

I was looking up code to be able to extract individual integers off a string of integers; I.E input: 400, output: cout << a << b << c; where a b c is the single lettering position of 400


Can someone explain this code to me? I tried doing the math and I get why you would use modulus 10, but then why divide the whole number by number/10?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int num;

	int a, b, c;

	cout << "Enter a number <1000: " << endl;
	cin >> num;

	if (num >= 1 && num <= 999)

	{
		a = num % 10;
		num /= 10;
		b = num % 10; 
		num /= 10;
		c = num % 10;
		num /= 10;

	}

	cout << c << b << a;
Modulus 10 will give you the rightmost digit. Dividing the number by 10 makes the second rightmost digit the new rightmost digit so you can then use modulus 10 to extract it.
Topic archived. No new replies allowed.