[go: up one dir, main page]

0% found this document useful (0 votes)
26 views3 pages

Solidity Programming Code

The document contains several Solidity programming examples, including a prime number checker, multi-level and single-level inheritance implementations, and a method to find the second largest element in an array. It also explains visibility and access qualifiers in Solidity through a contract demonstrating public, private, internal, and external functions. Each code snippet is structured to showcase specific programming concepts within the Solidity language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

Solidity Programming Code

The document contains several Solidity programming examples, including a prime number checker, multi-level and single-level inheritance implementations, and a method to find the second largest element in an array. It also explains visibility and access qualifiers in Solidity through a contract demonstrating public, private, internal, and external functions. Each code snippet is structured to showcase specific programming concepts within the Solidity language.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Solidity Programming Code

Q1) Write program in solidity to check whether a number is prime or not


pragma solidity ^0.8.0;
contract PrimeCheck {
function isPrime(uint num) public pure returns (bool) {
if (num <= 1) return false;
if (num == 2) return true;
for (uint i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

Q2) Write a program in solidity to implement multi-level inheritance


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract A {
function getA() public pure returns (string memory) {
return "This is Contract A";
}
}
contract B is A {
function getB() public pure returns (string memory) {
return "This is Contract B (inherits A)";
}
}
contract C is B {
function getC() public pure returns (string memory) {
return "This is Contract C (inherits B -> A)";
}
}

Q3) Write a program in solidity to find the second largest element in an array
pragma solidity ^0.8.0;
contract SecondLargest {
function findSecondLargest(uint[] memory arr) public pure returns (uint) {
require(arr.length >= 2, "Array must have at least 2 elements");
uint largest = 0;
uint second = 0;
for (uint i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
second = largest;
largest = arr[i];
} else if (arr[i] > second && arr[i] < largest) {
second = arr[i];
}
}
return second;
}
}

Q4) Write a program in solidity to implement single-level inheritance


pragma solidity ^0.8.0;
contract Parent {
function getParent() public pure returns (string memory) {
return "This is Parent Contract";
}
}
contract Child is Parent {
function getChild() public pure returns (string memory) {
return "This is Child Contract (inherits Parent)";
}
}

Q5) Write & elaborate a code in solidity to explain visibility & activity qualifiers
pragma solidity ^0.8.0;
contract VisibilityExample {
// --- State variable ---
uint public publicVar = 1; // Accessible everywhere
uint private privateVar = 2; // Accessible only within this contract
uint internal internalVar = 3; // Accessible within this & derived contracts
// --- Functions ---
function publicFunc() public pure returns (string memory) {
return "Public function: Accessible everywhere";
}
function privateFunc() private pure returns (string memory) {
return "Private function: Accessible only inside this contract";
}
function internalFunc() internal pure returns (string memory) {
return "Internal function: Accessible inside this and derived contracts";
}
function externalFunc() external pure returns (string memory) {
return "External function: Can only be called from outside";
}
// Function to test private inside same contract
function callPrivate() public pure returns (string memory) {
return privateFunc(); // Works here
}
// Function to test internal inside same contract
function callInternal() public pure returns (string memory) {
return internalFunc(); // Works here
}
}
contract Derived is VisibilityExample {
function accessInherited() public view returns (uint, string memory) {
// Access internal variable and function from parent
return (internalVar, internalFunc());
}
}

You might also like