473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

while loop

Hey guys I have a problem with this while loop, its not working properly and I think that I might be typing it wrong but I don't how. please help me! Thanks... Anyways this is a calculator which is suppose to find the approximated square root value and using this algorithm, it is suppose to work. However in the while loop it apparently doesn't work for me.

Heres the code I put in:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. int main ()
  5. {
  6.  
  7.    double second, diff;
  8.    double first = 1.0;     /* Given */
  9.    double number;
  10.  
  11.    printf("please enter the number: ");
  12.    scanf("%lf", &number);
  13.  
  14. /* the formula that I am suppose to use to find the approx sqrt*/
  15.  
  16.    second = .5 * (first + number/first);
  17.    diff = first - second;
  18.  
  19. /* while loop is suppose to run until the difference is less than .005*/
  20.    while (diff < .005)
  21.    {
  22. /* since the diff is greater than .005, we put in second into first  and then reiterating the same equation again*/
  23.       first = second;
  24.       second = .5 * (first + number/first);
  25.       diff = first - second;
  26.    }
  27.  
  28.    printf("\n%lf\n", second);
  29.    return 0;
  30. }
  31.  
  32.  
And when I plug in "3" for example, it comes up with 1.75, which it is suppose to be 1.73. But i have no idea how to change the loop to make that work. Thanks for helping!
Sep 29 '07 #1
4 1332
Ganon11
3,652 Expert 2GB
Something is wrong with your logic...For instance, when I run your program with the value 25, it gives me the answer as 7.461538. I suspect it's a problem with using number inside the loop, rather than a problem with the loop itself.
Oct 1 '07 #2
sicarie
4,677 Expert Mod 4TB
What do first and second stand for? Should you be resetting their values after the loop? I'd add a few cout statements in there and check what values are in all the variables each time you loop.
Oct 1 '07 #3
Ganon11
3,652 Expert 2GB
I'd add a few cout statements in there and check what values are in all the variables each time you loop.
Or printf, since this code looks very C-ish to me. :D
Oct 1 '07 #4
sicarie
4,677 Expert Mod 4TB
Or printf, since this code looks very C-ish to me. :D
Heh, yeah, that would work too... Whoops :/
Oct 1 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
10
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.