SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Flow Of Control
Exercise:
Programming Projects
Question:8 | ISBN:9780132846813 | Edition: 5

Question

Write a program that finds the temperature, as an integer, that is the same in both Celsius and Fahrenheit. The formula to convert from Celsius to Fahrenheit is as follows:

Fahrenheit = Celsius + 32

Your program should create two integer variables for the temperature in Celsius and Fahrenheit. Initialize the temperature to 100 degrees Celsius. In a loop, decrement the Celsius value and compute the corresponding temperature in Fahrenheit until the two values are the same.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program Code:

//Header section
#include <iostream>
using namespace std;

//main method
int main() 
{
    // Initialize the temperature to 100 degrees Celsius
    int celsius = 100; 
    //Declare variable
    int fahrenheit;

    // Decrement the Celsius value and compute the corresponding temperature in Fahrenheit
    while (true) 
    {
        // Convert Celsius to Fahrenheit using the formula
        fahrenheit = (9 * celsius) / 5 + 32;  
        // Check if the Fahrenheit and Celsius values are the same
        if (fahrenheit == celsius) 
        {
            break;  // Exit the loop if the values are the same
        }

        celsius--;  // Decrement the Celsius value for the next iteration
    }
    //Display output
    cout << "Temperature: " << celsius << " degrees Celsius / Fahrenheit" << endl;

    return 0;
}

 

OUTPUT OF THE PROGRAM CODE:

Temperature: -40 degrees Celsius / Fahrenheit

 

0 0

Discussions

Post the discussion to improve the above solution.