Dear All,
I have written a program that finds the integer whose square is closest to but greater than the integer number input as data. But it is giving me an erroneous answer. My algorithm is:
- Select a number which is half the input number.
- Square it and check whether it satisfies our condition.
- Subtract one and square it and check whether it satisfies our condition.
- Repeat 2 and 3 iteratively until a solution is obtained.
Below is my code for the above problem.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,m,input,powm,pown;
printf("Enter a number");
scanf("%d",&input);
m=input/2;
n=m-1;
powm=pow(m,2);
pown=pow(n,2);
if (powm>input)
{
if(pown>input)
{
m=m-1;
n=n-1;
powm=pow(m,2);
pown=pow(n,2);
}
}
printf("The integer whose square is closest to but greater than the input number is %d",n);
system("pause");
return 0;
}
Thank you for your reply.