- We can use the
/
operator for the division and the %
operator for the remainder:
import std.stdio;
void main() {
int first;
write("Please enter the first number: ");
readf(" %s", &first);
int second;
write("Please enter the second number: ");
readf(" %s", &second);
int quotient = first / second;
int remainder = first % second;
writeln(first, " = ",
second, " * ", quotient, " + ", remainder);
}
- We can determine whether the remainder is 0 or not with an
if
statement:
import std.stdio;
void main() {
int first;
write("Please enter the first number: ");
readf(" %s", &first);
int second;
write("Please enter the second number: ");
readf(" %s", &second);
int quotient = first / second;
int remainder = first % second;
write(first, " = ", second, " * ", quotient);
if (remainder != 0) {
write(" + ", remainder);
}
writeln();
}
-
import std.stdio;
void main() {
while (true) {
write("0: Exit, 1: Add, 2: Subtract, 3: Multiply,",
" 4: Divide - Please enter the operation: ");
int operation;
readf(" %s", &operation);
if ((operation < 0) || (operation > 4)) {
writeln("I don't know this operation");
continue;
}
if (operation == 0){
writeln("Goodbye!");
break;
}
int first;
int second;
write(" First number: ");
readf(" %s", &first);
write("Second number: ");
readf(" %s", &second);
int result;
if (operation == 1) {
result = first + second;
} else if (operation == 2) {
result = first - second;
} else if (operation == 3) {
result = first * second;
} else if (operation == 4) {
result = first / second;
} else {
writeln(
"There is an error! ",
"This condition should have never occurred.");
break;
}
writeln(" Result: ", result);
}
}
-
import std.stdio;
void main() {
int value = 1;
while (value <= 10) {
if (value != 7) {
writeln(value);
}
++value;
}
}