Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tanisha0708 patch 1 #1615

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions Blockchain Development/Basic/Arithmetic/Avg/avg.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,63 @@
pragma solidity 0.8.0;

contract Avg {
// Average of three numbers
int public avg ;
// Average of three numbers with precision handling
int public avg;

function getAvg( int num1 , int num2, int num3 ) public returns( int ){
int sum = num1 + num2 + num3 ;
avg = sum / 3 ;
return avg ;
function getAvg(int num1, int num2, int num3, uint decimals) public returns (int) {
int sum = num1 + num2 + num3;

// Calculate average with desired precision
// Multiply by 10^decimals to get the required precision
int precisionMultiplier = int(10 ** decimals);
avg = (sum * precisionMultiplier) / 3;

return avg;
}

function getFormattedAvg(uint decimals) public view returns (string memory) {
// Convert the average to string with the specified number of decimal places
int integerPart = avg / int(10 ** decimals);
int fractionalPart = avg % int(10 ** decimals);

// Format as a string
return string(abi.encodePacked(
intToString(integerPart),
".",
uintToString(uint(fractionalPart), decimals)
));
}

// Helper function to convert integer part to string
function intToString(int _i) internal pure returns (string memory) {
if (_i == 0) return "0";
bool negative = _i < 0;
uint i = uint(negative ? -_i : _i);
uint j = i;
uint length;
while (j != 0) {
length++;
j /= 10;
}
bytes memory bstr = new bytes(length + (negative ? 1 : 0));
uint k = bstr.length;
while (i != 0) {
bstr[--k] = bytes1(uint8(48 + i % 10));
i /= 10;
}
if (negative) {
bstr[0] = '-';
}
return string(bstr);
}

// Helper function to convert fractional part to string with fixed decimal places
function uintToString(uint _i, uint decimals) internal pure returns (string memory) {
bytes memory bstr = new bytes(decimals);
for (uint k = decimals; k > 0; k--) {
bstr[k - 1] = bytes1(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);
}
}
}
22 changes: 15 additions & 7 deletions Blockchain Development/Basic/Arithmetic/Cube/cube.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
pragma solidity 0.8.0;

contract Cube {
uint public number ;
uint public cube_ ;
function getCube( uint _number) public returns( uint ){
number = _number ;
cube_ = (number * number * number) ;
return cube_ ;
uint public number; // Stores the input number, if needed

// Function to calculate the cube of a number without storing the result on-chain
function getCube(uint _number) public pure returns (uint) {
require(_number <= 2**85, "Input is too large and may cause overflow."); // Simple overflow guard

uint result = _number * _number * _number;
return result;
}

// Function to set a number and get its cube
function setAndCube(uint _number) public returns (uint) {
number = _number;
return getCube(_number);
}
}
}
Loading