Two comparable loop but different results...help please

This is a discussion on Two comparable loop but different results...help please within the C Programming forums, part of the General Programming Boards category; declaration of variables and start of program: Code: #include<stdio.h> main() { int fahr, celcius; int upper, lower, step; lower = ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    127.0.0.1
    Posts
    9

    Two comparable loop but different results...help please

    declaration of variables and start of program:

    Code:
    #include<stdio.h>
    
    main()
    {
    	int fahr, celcius;
    	int upper, lower, step;
    
    	lower = 0;
    	step = 20;
    	upper = 300;
    loop one:

    Code:
    printf("\n");
    
    	for(celcius = lower; celcius <= upper; celcius = celcius + step)
    	{
    		fahr = 9 *(celcius) / 5 + 32;
    
    		printf("%d\t%d\n", fahr, celcius);
    	}
    loop two:

    Code:
    	printf("\n");
    
    	for(celcius = lower; celcius <= upper; celcius = celcius + step)
    	{
    		fahr = ((9/5) * celcius) + 32;
    
    		printf("%d\t%d\n", fahr, celcius);
    	}
    the first executes fine and gives good results for converting from F to C.

    the second executes fine but both fahr and celcius increment by 20.

    the math should give the same results unless i am just not seeing the algebra error (very possible as it is kinda late here)

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    fahr = ((9/5) * celcius) + 32;    // Integer division!
    You sidestepped this is the first loop, but not the second.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf is changing my results!
    By rogster001 in forum C Programming
    Replies: 8
    Last Post: 08-18-2009, 01:26 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21