The modulus operator which is represented by a % symbol is the second half of a division equation when only working with integers and not floats. This is to say that if you divide 11 by 10 you will get 1 as the result but there was a remainder of 1. The modulus lets us see the remainder instead of the result. In code it looks like this.
//This will return the quotient of the equation
int quotient = 11 / 1;
//This is the remainer and will be equal to 1
int remainder = 11 % 10;
This has some interesting uses for example it is an easy way to check if a number is even or odd or rather if it is divisible by any number because if the quotient equals zero then it is divisible by that number
//Check even or odd
if (num % 2 == 0)
return "Is even"
else
return "Not even"
Okay so I've talked about the modulus operator now what does this lab want us to do? It asks a few questions it wants us to ask the user for a four digit number, then to calculate the sum of those digits and then the sum of the digits of the sum and then the sum of the digits of the sum of the digits of the sum. That last part is confusing but is a direct quote from the lab. Let's look at an expected output to get a better idea of what we are being asked.
Please enter a four digit number : 7859
The sum of the digits is : 0029
The sum of the digits of the sum is : 0011
The sum of the digits of the sum of the digits is : 0002
Alright now that we can see what we are solving, how can the modulus operator be applied here. Well we can combine the division operator and the modulus. Say for example 7859 and we want to pop that 9 off the end?
int num = 7859
int fourthDigit = num%10 // this equals 9
int thirdDigit = num/10 % 10 // this equals 5
int secondDigit = num/100 % 10 // this equals 8
int firstDigit = num/1000 // this equals 7
In the above code we combine the two. For the fourth digit we can simply use the modulus operator to get the last digit with %10. If we look at the math it would be a quotient of 785 with a remainder of 9. Using division we can cut the number down in a sense. For example when we divide 7859/10 we get the 785 we just talked about but what happens if we then do %10 again? We get the 5 off the end. Repeating this step again for the second digit but instead dividing by 100. Something interesting happens when we get to the end of out number though. We could continue the pattern with 7859/1000%10 but if we look at that we realize it is unnecessary because 7859/1000 = 7 which is already the answer we need.
Putting all of this all together with what we learning in activity 2 will look something like this.
int sum, num1, num2, num3, num4;
cout << "Please enter a 4 digit integer." << endl;
cin >> sum;
num4 = sum % 10;
num3 = sum / 10 % 10;
num2 = sum / 100 % 10;
num1 = sum / 1000;
sum = num1 + num2 + num3 + num4;
cout << "Sum of the digits: " << num1 << " + " << num2 << " + " << num3 << " + " << num4 << " = " << sum << endl;
num4 = sum % 10;
num3 = sum / 10 % 10;
num2 = sum / 100 % 10;
num1 = sum / 1000;
sum = num1 + num2 + num3 + num4;
cout << "Sum of the digits of the sum: " << num1 << " + " << num2 << " + " << num3 << " + " << num4 << " = " << sum << endl;
num4 = sum % 10;
num3 = sum / 10 % 10;
num2 = sum / 100 % 10;
num1 = sum / 1000;
sum = num1 + num2 + num3 + num4;
cout << "Sum of the digits of the sum of the digits of the sum: " << num1 << " + " << num2 << " + " << num3 << " + " << num4 << " = " << sum << endl;
This could be made a lot better if we were allowed to use functions but as mentioned before this class has limitations in place and one of them prevents the use of functions.
This activity is a great example of how a simple operator combined with a little creativity and logic can solve problems effectively and efficiently.