Write a program which, given three integers initial, final and increment, prints a table of temperature conversions from Celsius to Fahrenheit. The table ranges from initial to final in steps of increment. {F = 32 + 9C/5}

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

int main(int argc, char **argv) {

    int initial, final, increment, i;
    printf("Enter the initial, final and increment values:\n");
    scanf("%d%d%d", &initial, &final, &increment);
    printf("+--------------------------------+\n");
    printf("|Table of Temperature Conversions|\n");
    printf("+--------------------------------+\n");
    printf("|     Celcius    |    Fahrenheit |\n");
    printf("+--------------------------------+\n");

    for (i = initial; i <= final; i+=increment) {
        printf("|%15d | %14.2f|\n", i, 32 + (float)(9*i)/5);
    }
    printf("+--------------------------------+\n");

    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

 

Brief Explanation

  • The program starts by requesting the user to enter the initial, final and increment values.
  • Using the function scanf, the values are read and assigned to variables initial, final and increment.
  • Using the for loop, a table of temperature conversions from Celsius to Fahrenheit is printed. The formula used for conversion is [32 + (9*i)/5], where i is the value in Celsius.

Add comment


Security code
Refresh