C Programming Assignment

docx

School

Institute of Space Technology, Islamabad *

*We aren’t endorsed by this school

Course

108408

Subject

Computer Science

Date

Nov 24, 2024

Type

docx

Pages

5

Uploaded by sahirnaseer

Report
P a g e | 1 Question 1 #include <stdio.h> // Include standard input/output library for printf and scanf int main() { // Declare variables to store Fahrenheit and Celsius temperatures float fahrenheit, celsius; // Prompt the user to input a temperature in Fahrenheit printf("Enter temperature in Fahrenheit: "); scanf("%f", &fahrenheit); // Convert Fahrenheit to Celsius using the formula (F - 32) * 5/9 celsius = (fahrenheit - 32) * 5.0 / 9.0; // Display the result in degrees Celsius printf("%.2f degrees Fahrenheit is equal to %.2f degrees Celsius.\n", fahrenheit, celsius); return 0; // Return 0 to indicate successful execution } Explanation This C program converts a temperature value from Fahrenheit to Celsius. It starts by declaring two variables to store the Fahrenheit and Celsius temperatures. Then, it prompts the user to input a temperature in Fahrenheit using the "printf" function to display a message and "scanf" to capture the user's input. After obtaining the Fahrenheit value, it performs the conversion to Celsius using the formula (Fahrenheit - 32) multiplied by 5/9. The result is stored in the "celsius" variable. Finally, it displays the converted temperature in degrees Celsius using the "printf" function. Output Question 2 #include <stdio.h> // Function prototype for temperature conversion double convTemp(double temperature, char conversionType); int main() { // Variable declaration double temperature, convertedTemperature; char conversionType; // Prompt the user to choose the conversion type printf("Choose conversion type:\n");
P a g e | 2 printf("Enter 'F' to convert Fahrenheit to Celsius or 'C' to convert Celsius to Fahrenheit: "); scanf(" %c", &conversionType); // Prompt the user to input a temperature printf("Enter temperature: "); scanf("%lf", &temperature); // Call the convTemp function to perform the conversion convertedTemperature = convTemp(temperature, conversionType); //printing the output if (conversionType == 'F') { printf("%.2lf degrees Fahrenheit is equal to %.2lf degrees Celsius.\n", temperature, convertedTemperature); } else if (conversionType == 'C') { printf("%.2lf degrees Celsius is equal to %.2lf degrees Fahrenheit.\n", temperature, convertedTemperature); } return 0; } // User-defined function to convert temperature double convTemp(double temperature, char conversionType) { if (conversionType == 'F') { // Convert Fahrenheit to Celsius return (temperature - 32.0) * 5.0 / 9.0; } else if (conversionType == 'C') { // Convert Celsius to Fahrenheit return (temperature * 9.0 / 5.0) + 32.0; } } Explanation This C program allows the user to convert temperatures between Fahrenheit and Celsius. The program starts by including the standard input/output library stdio.h and declaring a function prototype for convTemp, which will be used to convert temperatures. In the main function, it declares variables to store the temperature, converted temperature, and the conversion type. It then prompts the user to choose the conversion type F for Fahrenheit to Celsius or C for Celsius to Fahrenheit and input a temperature value. It calls the `convTemp` function, passing in the user's temperature and conversion type choice as arguments, and stores the result in the convertedTemperature variable. Depending on the chosen conversion type F or C, it prints the converted temperature along with an appropriate message. If Fahrenheit to Celsius is selected, it displays the temperature in Celsius, and if Celsius to Fahrenheit is chosen, it displays the temperature in Fahrenheit. The convTemp function takes the user's temperature and conversion type as input and performs the temperature conversion based on the selected type. For F (Fahrenheit to Celsius) conversion, it applies the formula (Fahrenheit - 32) * 5/9. For C (Celsius to Fahrenheit) conversion, it uses the formula (Celsius * 9/5) + 32.
P a g e | 3 Output Question 3 # include <stdio.h> int main () { int year ; // Ask the user for input printf ( "Enter a year: " ); scanf ( "%d" , & year ); // Check if the year meets the leap year conditions if (( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 )) { // It's a leap year printf ( "%d is a leap year.\n" , year ); } else { // It's not a leap year printf ( "%d is not a leap year.\n" , year ); } return 0 ; } Explanation This C program determines whether a given year is a leap year or not. The program starts by including the standard input/output library (stdio.h) and declares an integer variable named year to store the user's input. It prompts the user to enter a year using printf, and then captures the input year using scanf. The program checks if the input year meets the conditions listed below for being a leap year using an if statement. It is divisible by 4 but not divisible by 100. It is divisible by 400. If the input year satisfies the leap year conditions, it prints a message indicating that it's a leap year. Otherwise, it prints a message indicating that it's not a leap year.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
P a g e | 4 Output Question 4 # include <stdio.h> int main () { double num1 , num2 , num3 ; // Prompt the user to enter three numbers printf ( "Enter three numbers: " ); scanf ( "%lf %lf %lf" , & num1 , & num2 , & num3 ); // Check which number is the largest if ( num1 >= num2 && num1 >= num3 ) printf ( "%.2lf is the largest value\n" , num1 ); else if ( num2 >= num1 && num2 >= num3 ) printf ( "%.2lf is the largest value\n" , num2 ); else printf ( "%.2lf is the largest value\n" , num3 ); return 0 ; } Explanation This C program is designed to find and display the largest of three input numbers provided by the user. The program begins by including the standard input/output library (stdio.h) and declaring three double-precision floating-point variables (num1, num2, and num3) to store the user's input numbers. It prompts the user to enter three numbers using the printf function and captures these numbers using the scanf function. The format specifier %lf is used to read double-precision floating-point numbers. The program uses conditional statements (if, else if, and else) to compare the three input numbers and determine which one is the largest. It does this by comparing each number with the others using logical operators (>=). If a number is greater than or equal to the other two, it is considered the largest. After determining the
P a g e | 5 largest number, the program uses printf to display a message indicating which number is the largest, along with its value. Output