Skip to content
This repository has been archived by the owner on Oct 7, 2023. It is now read-only.

Create aiga9q.cpp #1582

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions mjibyi/aiga9q.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <cmath>

using namespace std;

// Function to calculate the square root using the Newton-Raphson method
double calculateSquareRoot(double number, double epsilon = 1e-6) {
double guess = number; // Initial guess
double prevGuess = 0; // Previous guess

while (abs(guess - prevGuess) > epsilon) {
prevGuess = guess;
guess = 0.5 * (guess + number / guess); // Newton-Raphson formula
}

return guess;
}

int main() {
double number;
cout << "Enter a number to calculate its square root: ";
cin >> number;

if (number < 0) {
cout << "Square root is not defined for negative numbers." << endl;
} else {
double sqrtResult = calculateSquareRoot(number);
cout << "The square root of " << number << " is approximately " << sqrtResult << endl;
}

return 0;
}