Learn Solidity - 10 Code Examples & CST Typing Practice Test
Solidity is a statically typed, contract-oriented programming language designed for building smart contracts on the Ethereum blockchain. It enables developers to write decentralized applications (dApps), manage digital assets, and automate trustless logic using a syntax inspired by JavaScript, C++, and Python.
View all 10 Solidity code examples →
Learn SOLIDITY with Real Code Examples
Updated Nov 19, 2025
Code Sample Descriptions
Solidity Counter Contract
pragma solidity ^0.8.0;
contract Counter {
uint public count = 0;
bool public isDark = false;
function increment() public {
count++;
}
function decrement() public {
count--;
}
function reset() public {
count = 0;
}
function toggleTheme() public {
isDark = !isDark;
}
function getUI() public view returns (uint, bool) {
return (count, isDark);
}
}
Demonstrates a simple counter smart contract with theme toggle functionality simulated as a boolean.
Solidity Simple Addition
pragma solidity ^0.8.0;
contract AddProgram {
uint public a = 5;
uint public b = 3;
uint public sum;
function calculate() public {
sum = a + b;
}
}
Adds two numbers and stores the result in a state variable.
Solidity Factorial
pragma solidity ^0.8.0;
contract Factorial {
uint public n = 5;
uint public fact = 1;
function calculate() public {
fact = 1;
for(uint i=1;i<=n;i++) {
fact *= i;
}
}
}
Calculates factorial of a number using a loop.
Solidity Fibonacci Sequence
pragma solidity ^0.8.0;
contract Fibonacci {
uint[10] public fib;
function generate() public {
fib[0] = 0;
fib[1] = 1;
for(uint i=2;i<10;i++) {
fib[i] = fib[i-1] + fib[i-2];
}
}
}
Generates first 10 Fibonacci numbers stored in an array.
Solidity Max of Two Numbers
pragma solidity ^0.8.0;
contract MaxProgram {
uint public a = 7;
uint public b = 10;
uint public max;
function findMax() public {
max = a > b ? a : b;
}
}
Finds the maximum of two numbers.
Solidity Array Sum
pragma solidity ^0.8.0;
contract ArraySum {
uint[5] public nums = [1,2,3,4,5];
uint public sum;
function calculateSum() public {
sum = 0;
for(uint i=0;i<nums.length;i++) {
sum += nums[i];
}
}
}
Sums elements of an array and stores in a state variable.
Solidity Even Numbers Filter
pragma solidity ^0.8.0;
contract EvenNumbers {
uint[10] public nums = [1,2,3,4,5,6,7,8,9,10];
uint[] public evens;
function filter() public {
delete evens;
for(uint i=0;i<nums.length;i++) {
if(nums[i] % 2 == 0) {
evens.push(nums[i]);
}
}
}
}
Returns even numbers from an array.
Solidity String Concatenation
pragma solidity ^0.8.0;
contract ConcatStrings {
string public str1 = 'HELLO';
string public str2 = 'WORLD';
string public result;
function concat() public {
result = string(abi.encodePacked(str1, str2));
}
}
Concatenates two strings and stores in a state variable.
Solidity Counter With Loop Simulation
pragma solidity ^0.8.0;
contract LoopCounter {
uint public count;
function run() public {
count = 1;
while(count <= 5) {
count++;
}
}
}
Counts from 1 to 5 using a loop and stores the final count.
Solidity Conditional Increment
pragma solidity ^0.8.0;
contract ConditionalIncrement {
uint public count = 3;
function run() public {
if(count < 5) {
count++;
}
}
}
Increment counter only if it is less than 5.
Frequently Asked Questions about Solidity
What is Solidity?
Solidity is a statically typed, contract-oriented programming language designed for building smart contracts on the Ethereum blockchain. It enables developers to write decentralized applications (dApps), manage digital assets, and automate trustless logic using a syntax inspired by JavaScript, C++, and Python.
What are the primary use cases for Solidity?
Smart contract development. DeFi protocols (DEXs, lending, staking). NFT standards (ERC-721, ERC-1155). Token creation (ERC-20). DAO governance mechanisms. Permissioned enterprise blockchain apps
What are the strengths of Solidity?
Native support for Ethereum standards. Large community & ecosystem. Powerful for financial logic. Extensive tooling (Hardhat, Foundry, Truffle). Rich developer resources
What are the limitations of Solidity?
Security vulnerabilities are common. Gas-cost constraints. Upgradability complexity. Difficult debugging across chains. Blockchain immutability adds risk
How can I practice Solidity typing speed?
CodeSpeedTest offers 10+ real Solidity code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.