Remainders
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 14 of 53.
The % operator returns the remainder after integer division. Use it to find leftovers after making equal groups. For nonnegative values and a positive divisor, the remainder is at least zero and smaller than the divisor.
The statements in this excerpt run inside the provided main function.
uint256 beads = 22;
console.log(beads % 5);Four complete groups of five use twenty beads, leaving two. The remainder answers a different question from division: % gives leftovers, while / gives complete groups. A divisor of zero is invalid for either operation.
For a nonnegative value and a positive divisor, the remainder is smaller than the divisor.
Challenge
EasyPrint how many loose pieces remain after grouping pieces into groups of size. size is greater than zero.
The tests pass arguments to main in this order: uint256 pieces, uint256 size. 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 pieces, uint256 size) 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