Skip to main content

C Program using While loop to Convert Fahrenheit to Celsius

This is a C program to convert Fahrenheit to Celcius.
Let us take a look at the program .
This can be easily accomplished with a for loop or a while loop:

#include 

int main()
{
int a;
a = 0;
while (a <= 100)
{
printf("%4d degrees F = %4d degrees C\n",
a, (a - 32) * 5 / 9);
a = a + 10;
}
return 0;
}

If you run this program, it will produce a table of values starting at 0 degrees F and ending at 100 degrees F. The output will look like this:

   0 degrees F =  -17 degrees C
10 degrees F = -12 degrees C
20 degrees F = -6 degrees C
30 degrees F = -1 degrees C
40 degrees F = 4 degrees C
50 degrees F = 10 degrees C
60 degrees F = 15 degrees C
70 degrees F = 21 degrees C
80 degrees F = 26 degrees C
90 degrees F = 32 degrees C
100 degrees F = 37 degrees C

The table's values are in increments of 10 degrees. You can see that you can easily change the starting, ending or increment values of the table that the program produces.

If you wanted your values to be more accurate, you could use floating values instead of integer values:

#include 

int main()
{
float a;
a = 0;
while (a <= 100)
{
printf("%6.2f degrees F = %6.2f degrees C\n",
a, (a - 32.0) * 5.0 / 9.0);
a = a + 10;
}
return 0;
}

You can see that the declaration for a has been changed to a float, and the %f symbol replaces the %d symbol in the printf statement. In addition, the %f symbol has some formatting applied to it: The value will be printed with six digits preceding the decimal point and two digits following the decimal point.

Now let's say that we wanted to modify the program so that the temperature 98.6 is inserted in the table at the proper position. That is, we want the table to increment every 10 degrees, but we also want the table to include an extra line for 98.6 degrees F because that is the normal body temperature for a human being. The following program accomplishes the goal:

#include 

int main()
{
float a;
a = 0;
while (a <= 100)
{
if (a > 98.6)
{
printf("%6.2f degrees F = %6.2f degrees C\n",
98.6, (98.6 - 32.0) * 5.0 / 9.0);
}
printf("%6.2f degrees F = %6.2f degrees C\n",
a, (a - 32.0) * 5.0 / 9.0);
a = a + 10;
}
return 0;
}

Comments

Fine. I have a blog site on C Tutorials also. Pl. visit My Blhttp://chakra2009.blogspot.comg
Fine. I have a blog site also My Blog
Anonymous said…
Hey I know this is off topic but I was wondering if you knew
of any widgets I could add to my blog that automatically
tweet my newest twitter updates. I've been looking for a plug-in
like this for quite some time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy
reading your blog and I look forward to your new updates.


My web page: book of ra kostenlos online spielen ohne registrierung ()
G.K. said…
Hi , thanks for the comments.
But I am not aware of any widgets like that at the moment.

Popular posts from this blog

Turbo C++ for Windows 7 64 bit

 Its a very simple guide to install Turbo C++ on Windows 7 64 bit  operating system. Follow the steps below to install Turbo C++ compiler on your windows 7 ,64 bit operating system. Note - Please let me know whether the process is working for you or if you are getting some kind of error. Step 1) Install the software DOSBOX version 0.73(Search for it in the search bar). Step 2) Create a folder on your C drive, e.g.Turbo; (c:\Turbo\) Step 3) Extract TC into the created folder(e.g. c:\Turbo\);(you can search and download it) Step 4) Run the application DOSBOX 0.73,previously downloaded and installed. Step 5) Type the following commands at the command prompt that will appear, [z]: mount d c:\Turbo\ Following this you will get a message "Drive D is mounted as local directory c:\Turbo\" Step 6) Type d: to shift to d: Step 7) Then type commands mentioned below; cd tc cd bin tc or tc.exe Step 8) In the turbo C++ go to Options\Directories\ Chang

Calculating the average of two numbers using C++

Though this site aims at understanding C language, I am writing a program for you in C++, to have a general idea about C++ too. A program in C++,to calculate the average of two numbers, which are entered by the keyboard while the execution of the program. Try to understand the program; #include &lt iostream &gt using namespace std; int main() {     float num1,num2,average,sum;     cout << "Enter the first number:";     cin >> num1;     cout << "Enter the second number:";     cin >> num2;     sum=num1+num2;     average=sum/2;     cout << "The sum of the two numbers is:"<< sum<<"\n"<<"The average of the two numbers is:" << average; return(0); } Explaination: The new file header used here in this C++ program is the #include . By this , we are asking the preprocessor to add the contents of the iostream file to our program. The program is written in the

Finding the arithmetic operators

Below is a very simple program to find the arithmetic operator. Please feel free to put a comment if need an explanation. #include #include void main()   {     float a1,a2;     char operator;     printf("\n Enter the values of a1 operator a2\n");     scanf("%f%c%f",&a1,&operator,&a2);     switch(operator)      { case '+': printf("%.2f",a1+a2); break; case '-':  printf("%.2f",a1-a2);  break; case '*':  printf("%.2f",a1*a2); break; case '/':  if(a2==0)  printf("error\n");  else  printf("%.2f",a1/a2);  break; default: printf("unknown operatotr\n"); break;      }    }