Remainder Concept C++ - ProgrammerTech
Language:

Search

# Remainder Concept C++

## Remainder C++

Remainder, in this lesson we will apply more applications to the if statement and we will learn. On a mathematical tool called the remainder of the division Remainder and we will make some applications on it.

The remainder theorem is a very important concept in programming that we will understand in this lesson, and it is the remainder of the long division process.

example 1

``````#include <iostream>
using namespace std;
int main()
{
int x = 22;
int y;
cout << "Enter the value for y : ";
cin >> y;
cout << x << " / " << y << " = " << x / y << endl;
return 0;
}``````

We created an integer variable x whose value is 22 and then created an unknown variable y to prompt the user to enter.

Then we built the print function cout and we asked the program to print x and then put the division sign / then the = sign and then divide x by y and then show the result on the screen.

And when you run the program and enter the number 2, the result will be like:

``````Enter the value for y : 2
22 / 2 = 11``````

Example 2

``````#include <iostream>
using namespace std;
int main()
{
int x = 22;
int y;
cout << "Enter the value y : ";
cin >> y;
cout << x << " / " << y << " = " << x / y << endl;
cout << x << " % " << y << " = " << x % y << endl;
return 0;
}``````

We have created an integer variable x with a value of 22 and then created an unknown variable y to prompt the user to enter.

Then we built the print function cout and we asked the program to print x and then put the division sign / then the = sign and then divide x by y and then show the result on the screen.

Then we set a second print function and asked the program to print x and then put a sign with the division %.

Then the = sign, and then it calculates the remainder of the division of x by y and shows the result after that on the screen.

And when you run the program and enter the number 3, the result will be like:

``````Enter the value y : 3
22 / 3 = 7
22 % 3 = 1``````

In mathematics, we learned the method of long division, and if we wanted to divide 3 by two, we would see how many two are in the 3 and there would be two and one in the three.

So the product of long division is 1 then we complete by multiplying the product by the divisor 1 multiplied by 2 equals 2, then we subtract the divisor from 3 minus two equals 1.

## Remainder Properties C++

There are five 5 important characteristics of the remainder of the division, which we will explain in detail in order to understand them, and these characteristics are:

Firstly:

If the remainder of the Remainder division of one number by another number is 0, this means that the first number is divisible by the second number.

For example, 10 remains divisible by 5 is equal to 0 because 10 is divisible by 5, and 10 remains divisible by 6 is not equal to zero, so it is not divisible by it.

Secondly:

Remainder of (A) Let's say A %2 = And let's say A is any number over two that distinguishes the number whether it is even or odd.

If the result is 0, it is an even number, but if the result is 1 it is an odd number, this property is the result of the first property, as any number that is divisible by 2 is an even number.

Third:

Suppose A % B = ? The result of the remainder of A is any number over the variable B means any other number. It is always less than B since the remainder cannot be greater than the divisor.

Thus, if we complete 7, 8, and 9, it will always produce less than 3, and this property tells us that all . The resulting numbers, no matter how large the first number, will be between 0 and less than the divisive number.

Therefore, in the property of an even and odd number, we were dividing by 2, so the remainder of the division by 2 will be confined between 0 and 1, for example, 7%2 = 1, meaning that there are two options, either even or odd.

Fourthly:

If we divide a normal number by 10, we will remove the last digit in the ones place, for example 400/10 = 40.

We have removed the last digit of the ones 0 and the same result will be for the division of integers eg 401/10 = 40.

It does not preserve decimals with respect to the number 401 ie we remove the number in the ones place.

As for the remainder of the division, the number we removed from the ones place will be the same for 402.

For example, in this way, we can find out how much the ones place is in any number, and we can later decompose the number to more than one place.

fifth:

Suppose A%B=A If the number B is greater than the number A, which is not divisible by it, the result is always. For example, A will equal 3%9=3 three remainders divisible by nine is 3 because 2 is not divisible by 9.

example 3

Write a program that calculates the number of weeks in a year and then calculate how many odd days are left except for weeks.

``````#include <iostream>
using namespace std;
int main()
{
int days = 365;
cout << days / 7 << "weeks";
cout << " &" << days % 7 << " days" << endl;
return 0;
}``````

A numerical variable called days and its value 365 was created, which is the number of days in the year. We set a print function that prints the result of dividing the number of days in the year by 7 and then prints the word weeks, which means a week.

And we put a second printing function for the remainder of the division and put a sign & then it calculates the remainder of the division from the number of days in the year in 7 and then writes the word days any day.

And when you run the code, it will appear as follows.

``52weks &1 days``

We notice from the previous picture that it was written for us that the number of weeks in a year is 52 weeks, and the remainder of the division is 1 day.

Example 4

Make a program that reverses the number entered, provided that the number entered is composed of only three digits.

``````#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Write the three-digit number" << endl;
cin >> x;

if(x >= 100 && x <= 999)
{
//correct number
int a1 = x % 10;
int a2 = (x / 10) % 10;
int a3 = x / 100;
cout << a1 << a2 << a3 << endl;
}
else {
//wrong number
cout << "Bye, the number is wrong" << endl;
exit(1);
}
return 0;
}``````

We created a scalar variable int which is x and its value will be entered by the user, then printed to the user. A message telling him to compulsorily enter a three-digit number until it is reversed.

Next we set a function cin which will ask the user to enter the value of x and receive it.

And we set a condition via the if function if x is greater than or equal to 100 && x is less than or equal to 999.

This is in order for the user to be forced to type a number between 100 and 999, and if otherwise is entered, he will be expelled from the program.

And the condition was that if the number is true, the following is executed:

We created three variables, a1 for the ones place, a2 for the tens place, and a3 for the hundreds place.

And their conditions were in the ones place. We extract the value of the ones based on the fourth property from the previous properties.

And for the tens place a2 we also rely on the fourth property to extract the tens digit via (x/10) %10.

And then in the third column, the hundreds digit, we divide x by 100, because if we divide by 100, the program will delete the ones digit and the tens digit and keep the hundreds digit.

If you enter a valid value

If an incorrect value is entered,