In: Computer Science
In C Write a program that
Note : c = (5.0/9.0)*(f-32.0)
Sample output
Enter temperature in deg F
50
You entered 50 def F. The corresponding temp in deg C is 10.00
Another temperature conversion?
Enter y or n please
y
Enter temperature in deg F
60
You entered 60 def F. The corresponding temp in deg C is 15.56
Another temperature conversion?
Enter y or n please
a
Enter y or n please
#
Enter y or n please
y
Enter temperature in deg F
70
You entered 70 def F. The corresponding temp in deg C is 21.11
Another temperature conversion?
Enter y or n please
n
Bye
Problem 3
Write a program that
Note : c = (5.0/9.0)*(f-32.0)
Sample output
Enter temperature in deg F
50
You entered 50 def F. The corresponding temp in deg C is 10.00
Another temperature conversion?
Enter y or n please
y
Enter temperature in deg F
60
You entered 60 def F. The corresponding temp in deg C is 15.56
Another temperature conversion?
Enter y or n please
a
Enter y or n please
#
Enter y or n please
y
Enter temperature in deg F
70
You entered 70 def F. The corresponding temp in deg C is 21.11
Another temperature conversion?
Enter y or n please
n
Bye
Here is the complete c code with comments to understand everything:
//Inlcuding the required header files
#include <stdio.h>
#include <stdlib.h>
int main()
{
//tempinF is a float value so that decimals can be displayed
float tempInF;
//Initially we keep the choice value a 'y' so that we can enter the loop
char choice = 'y';
while (choice == 'y')
{
printf("Enter temperature in deg F\n");
scanf("%f", &tempInF);
/*we use %0.02f so that only 2 decimal places are displayed,
the formula as given in the question is applied to get the temperature in deg C*/
printf("You entered %0.02f def F. The corresponding temp in deg C is %0.02f", tempInF, (5.0 / 9.0) * (tempInF - 32.0));
printf("\nAnother temperature conversion?\nEnter y or n please\n");
/*getchar() is used here to clear the buffer, to better understand why it
is used try removing it and see what happens, then you will better understand
why it is used*/
getchar();
//Get the choice from the user
scanf("%c", &choice);
/*If the choice is n simply say bye and exit*/
if (choice == 'n')
{
printf("Bye\n");
exit(0);
}
/*Otherwise if the choice is not y then keep asking for a valid input until the
user inputs 'y' or 'n'*/
else if (choice != 'y')
{
while (choice != 'y' && choice != 'n')
{
getchar();
printf("Enter y or n please\n");
scanf("%c", &choice);
}
if (choice == 'n')
{
printf("Bye\n");
exit(0);
}
}
}
}
Here is the image of the code to understand the indentation:
Here is the output of the above code:
NOTE: If there is any doubt feel free to put it in the comments, I will be there to solve all doubts. If you like the answer a thumbs up would make my day ?