# celcius to farenheit convertor with for loop

Page 1 of 1

## 1 Replies - 3042 Views - Last Post: 13 November 2010 - 12:53 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=199911&amp;s=f66920af22eb3bdb946b3a6e06a456dd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 c infant

Reputation: 0
• Posts: 3
• Joined: 13-November 10

# celcius to farenheit convertor with for loop

Posted 13 November 2010 - 09:19 AM

Hi all,
I am having a problem with this convertor.
My remit is:
Design a C program to solve the following problem:

A program is required that will convert degrees Celsius to degrees Fahrenheit. The user must enter a start and a finish value together with a step value. The program must convert all temperatures beginning with the start value at the given step interval.

Use the “for” statement for this problem.

Example Input:
Input the start value: 10
Input finish value: 16
Input step value: 2

Desired output:
Celsius Fahrenheit
10 50.0
12 53.6
14 57.2
16 60.8

This is what I have so far:

```// steped celcius to farenheit convertor
// Author c infant

#include <stdio.h>
#include <stdlib.h>

main ()

{
float c1,c2,step,celcius,farenheit;

printf("Enter celcius value:");        // enter first variable and scan for input
scanf("%f",&c1);

printf("Enter Finish value:");        // enter 2nd variable and scan for input
scanf("%f",&c2);

printf("Enter Step value:");           // enter step variable and scan for input
scanf("%f",&step);

printf("\tcelcius\t  farenheit\n");   // print celcius / farenheit headings

for(c1+=step;c1<=c2)                 // looping conditions

{
farenheit=1.8*celcius+32            // manipulation of variables
printf("\t%.1f\t",celcius);             // print results
printf ("%.1f\t",farenheit);
}                                         // end loop

}                                        // end program
```

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 13 November 2010 - 09:52 AM

Is This A Good Question/Topic? 0

## Replies To: celcius to farenheit convertor with for loop

### #2 Coding in the name of

Reputation: 67
• Posts: 232
• Joined: 07-July 10

## Re: celcius to farenheit convertor with for loop

Posted 13 November 2010 - 12:53 PM

Here's a modified version of your code. I fixed the for syntax, added error checks, fixed the text output, and made the formatting more consistent and readable. Good formatting is something a lot of new coders wrongly neglect.

```// steped celcius to farenheit convertor
// Author c infant

#include <stdio.h>
#include <stdlib.h>

int main()
{
float c1, c2, step;

printf("Enter start value: ");	// Put spaces after colon
scanf("%f", &c1);

printf("Enter finish value: ");
scanf("%f", &c2);

printf("Enter step value: ");
scanf("%f", &step);

printf("celcius\tfarenheit\n");

if (step <= 0.0f)
{
// Error: if the loop begins it will never exit. Report and exit or correct and continue
}

if (c1 > c2)
{
// Error as above
}

// Format for for statement is for (init-expr; condition-expr; increment-expr)
//  v- Need a null statement here to indicate no init expression
for(; c1<=c2; c1+=step)
{
// In general variables should be in as close a scope as possible
float farenheit = 1.8f * c1 + 32.0f;		// c1 holds the current celsius value, not the celsius variable you had which was not initialised
printf("%.1f\t%.1f\n", c1, farenheit);
}

system("PAUSE");

return 0;
}
```