0% found this document useful (0 votes)
47 views4 pages

Solidity Programs

The document contains three Solidity programs: one for finding the second largest element in an array, another for checking if a number is prime, and a third demonstrating single inheritance. Each program includes a contract definition, function explanations, and details on handling edge cases or efficiency. The explanations clarify the purpose and mechanics of each contract and function.

Uploaded by

mabhijeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views4 pages

Solidity Programs

The document contains three Solidity programs: one for finding the second largest element in an array, another for checking if a number is prime, and a third demonstrating single inheritance. Each program includes a contract definition, function explanations, and details on handling edge cases or efficiency. The explanations clarify the purpose and mechanics of each contract and function.

Uploaded by

mabhijeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1)Write a program in solidity to find the second

largest element in an array


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SecondLargest {

// Function to find the second largest element in the array


function findSecondLargest(int[] memory arr) public pure returns (int) {
require([Link] >= 2, "Array must have at least 2 elements");

int largest = arr[0];


int secondLargest = arr[1];

// Initialize largest and secondLargest correctly


if (secondLargest > largest) {
(largest, secondLargest) = (secondLargest, largest);
}

// Iterate through the array to find the two largest values


for (uint i = 2; i < [Link]; i++) {
if (arr[i] > largest) {
secondLargest = largest; // Shift down the largest
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i]; // Update only if unique and smaller than largest
}
}

return secondLargest;
}
}

Explanation:
1. Contract Definition:
The contract is named SecondLargest.
2. Function findSecondLargest:
○ Takes an integer array (int[] memory arr) as input.
○ Checks that the array has at least 2 elements using require.
○ Initializes the first two elements as largest and secondLargest.
○ Iterates over the rest of the array to update the largest and secondLargest
values.
○ Returns the second largest element.
3. Handling Edge Cases:
○ The function ensures the input array has at least two elements.
○ It handles cases where there are duplicate largest values correctly.

2) Write a program in solidity to find whether


number is prime or not
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract PrimeChecker {

// Function to check whether a number is prime


function isPrime(uint num) public pure returns (bool) {
if (num < 2) {
return false; // Numbers less than 2 are not prime
}

// Check for divisibility up to the square root of the number


for (uint i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false; // If divisible by any number, not prime
}
}

return true; // If no divisors found, it's prime


}
}
Explanation:
1. Contract Definition:
The contract is named PrimeChecker.
2. Function isPrime:
○ It accepts an unsigned integer (uint) as input.
○ If the input is less than 2, it returns false (since 0 and 1 are not prime).
○ It checks for divisibility from 2 to the square root of the number to determine if
it’s prime.
■ If the number is divisible by any of these values, the function returns
false.
○ If no divisors are found, the function returns true.
3. Time Complexity:
The loop runs up to the square root of num for efficiency, making the complexity O(√n).

3)Write a program in solidity to implement single


inheritance

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Parent contract
contract ParentContract {
string public name;

// Constructor to initialize the name


constructor(string memory _name) {
name = _name;
}

// Public function to return a greeting


function greet() public view returns (string memory) {
return string([Link]("Hello, ", name, "!"));
}
}

// Child contract inheriting from ParentContract


contract ChildContract is ParentContract {
// Constructor of the child contract
constructor(string memory _name) ParentContract(_name) {}

// Additional function in the child contract


function welcomeMessage() public view returns (string memory) {
return string([Link]("Welcome to the system, ", name, "!"));
}
}

Explanation:

1. ParentContract:
○ The ParentContract contains:
■ A state variable name.
■ A constructor to initialize name when the contract is deployed.
■ A greet() function that returns a greeting message.
2. ChildContract:
○ The ChildContract inherits from ParentContract using the is
keyword.
○ It calls the parent constructor using ParentContract(_name) to
initialize the inherited name variable.
○ It defines an additional function welcomeMessage() to extend the
functionality.
3. Inheritance Mechanism:
○ The child contract gains access to the state variables and functions of the
parent contract, and it can extend or modify the functionality if needed.

You might also like