Addition and Subtraction
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 11 of 53.
Use + to combine quantities and - to remove one quantity from another. Store the result in a variable or pass the expression to the console. Solidity 0.8 checks integer arithmetic: a result outside the type range reverts instead of silently wrapping.
The statements in this excerpt run inside the provided main function.
uint256 stock = 18;
uint256 remaining = stock - 5;
console.log(remaining + 2);Removing five from eighteen leaves thirteen. Adding two gives fifteen. Unsigned subtraction is valid only when its result is nonnegative. The practice tasks state bounds that keep the operations valid.
Unsigned subtraction must not produce a negative result.
Challenge
EasyA shelf receives delivery books and lends borrowed books. Given the starting stock, print stock + delivery - borrowed. The inputs guarantee borrowed does not exceed stock plus delivery.
The tests pass arguments to main in this order: uint256 stock, uint256 delivery, uint256 borrowed. Each test starts with fresh contract state.
Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.
Try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
function main(uint256 stock, uint256 delivery, uint256 borrowed) external view {
// Write your solution here.
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
Practice on your own: Solidity playground