Chapter 2. Fundamental of ‘C’
Chapter 2. Fundamental of ‘C’
Question 1. Write a C program to convert Celsius to Fahrenheit and vice versa
Program :
#include <stdio.h>
void main()
{
float fahrenheit,celsius; // Declaration of variables with type float
int choice; // Declaration of variables with type int
printf("1: Convert temperature from Fahrenheit to Celsius."); // Simple print statements
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): "); // offering the choice to perform the task.
scanf("%d",&choice); // run time initialization of choice variable
if(choice ==1) // if user select 1 than this block will execute.
{
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fahrenheit); // run time initialization of choice variable
celsius= (fahrenheit - 32) / 1.8; // logic or equation to find Celsius
printf("Temperature in Celsius: %.2f",celsius); // printing celsius upto 2 decimal digits using %.2f
}
else if(choice ==2) // if user select 2 than this block will execute.
{
printf("\nEnter temperature in Celsius: ");
scanf("%f",&celsius);
fahrenheit= (celsius*1.8)+32; // logic or equation to find Fahrenheit
printf("Temperature in Fahrenheit: %.2f",fahrenheit); // printing Fahrenheit upto 2 decimal digits using %.2f
}
else // if user select neither 1 nor 2 than this block will execute.
{
printf("\nInvalid Choice !!!");
}
}
Program :
#include <stdio.h>
void main()
{
float fahrenheit,celsius; // Declaration of variables with type float
int choice; // Declaration of variables with type int
printf("1: Convert temperature from Fahrenheit to Celsius."); // Simple print statements
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): "); // offering the choice to perform the task.
scanf("%d",&choice); // run time initialization of choice variable
if(choice ==1) // if user select 1 than this block will execute.
{
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fahrenheit); // run time initialization of choice variable
celsius= (fahrenheit - 32) / 1.8; // logic or equation to find Celsius
printf("Temperature in Celsius: %.2f",celsius); // printing celsius upto 2 decimal digits using %.2f
}
else if(choice ==2) // if user select 2 than this block will execute.
{
printf("\nEnter temperature in Celsius: ");
scanf("%f",&celsius);
fahrenheit= (celsius*1.8)+32; // logic or equation to find Fahrenheit
printf("Temperature in Fahrenheit: %.2f",fahrenheit); // printing Fahrenheit upto 2 decimal digits using %.2f
}
else // if user select neither 1 nor 2 than this block will execute.
{
printf("\nInvalid Choice !!!");
}
}
Comments
Post a Comment