Question

In: Computer Science

In C Write a program that prompts the user to enter a Fahrenheit temperature calculate the...

In C Write a program that

  1. prompts the user to enter a Fahrenheit temperature
  2. calculate the corresponding temperature in Celsius and print it
  3. prompt the user if they want to do another temperature conversion
  4. if the user enters y, repeat 1), 2) and 3)
  5. if the user enters n, then print Bye and exit the program
  6. if the user enters any other character, ask the user to enter y or n only

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

  1. prompts the user to enter a Fahrenheit temperature
  2. calculate the corresponding temperature in Celsius and print it
  3. prompt the user if they want to do another temperature conversion
  4. if the user enters y, repeat 1), 2) and 3)
  5. if the user enters n, then print Bye and exit the program
  6. if the user enters any other character, ask the user to enter y or n only

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

Solutions

Expert Solution

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 ?


Related Solutions

IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal...
Computer Architecture and Organization(c++) Write a C++ program that prompts the user to enter a hexadecimal value, multiplies it by ten, then displays the result in hexadecimal. Your main function should a) declare a char array b) call the readLn function to read from the keyboard, c) call a function to convert the input text string to an int, d) multiply the int by ten, e) call a function to convert the int to its corresponding hexadecimal text string, f)...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
C programming. Write a program that prompts the user to enter a 6x6 array with 0...
C programming. Write a program that prompts the user to enter a 6x6 array with 0 and 1, displays the matrix, and checks if every row and every column have the even number of 1’s.
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT