Thread: Two comparable loop but different results...help please

  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
    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, 12:26 PM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12: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, 03:45 PM