Write a program that will find the squareroot of a positive integer number using
ID: 3807003 • Letter: W
Question
Write a program that will find the squareroot of a positive integer number using the technique described on the following website. http://www.math.com/school/subjectl/lessons/SllllL9DP.html I repeated step 2 and 3 ten times. Make your program display the information shown in the following sample runs. Requirement: Put all your code in the main function. [kslott@empress cslll_lec_falU6]$ ./a.out Enter an integer number that you want to find the squareroot for: -2 This program finds the squareroot of a number greater than 0 only. Enter a positive integer number: 8 Start at 3 The squareroot of 8 is 2.82843 The square of the squareroot is 8 [kslott@ empress cslll_lec_falll6]$ ./a.out Enter an integer number that you want to find the squareroot for: 25 for: 25 5 is the squareroot of 25Explanation / Answer
#include<stdio.h>
#include<math.h>
int main()
{
double n;
do
{
printf("Enter an integer number that you want to find the square root:");
scanf("%lf",&n);//reading number
if(n<=0)
printf("This program finds the square root of Number greater than 0 onlyt Enter a positive number... ");
}while(n<=0);//loop runs upto correct number is entered
int i=1;
while((i*i)<n)//finding a nearer perfect square
{
i++;
}
//printf("%d ",i);
double k = n-((i-1)*(i-1));//previous number..
double p = (i*i)-n;///nearer perfect squares..
//printf("%lf %lf ",k,p);
if(k<p)
i=i-1;//finding nearest perfect square
k=n/i;
double l=i;
//printf("%lf %lf %lf ",l,k,p);
while(( ceil( l*l* 100.0 ) / 100.0)!= ceil( n * 100.0 ) / 100.0)//finding square
{
// printf(" %lf %lf ",( ceil( l*l * 100.0 ) / 100.0),ceil( n * 100.0 ) / 100.0);
k=n/k;
p=(k+l)/2;
//printf("%lf %lf %lf ",l,k,p);
l=k;
k=p;
}
printf("The square root of %lf is : %lf ",n,l);
printf("The square of square root is %ld ",(int)ceil(l*l));
return 0;
}
output:-
Enter an integer number that you want to find the square root:10
The square root of 10.000000 is : 3.162248
The square of square root is 10
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.