Tutorial 7
👉
1.Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal.
#include <stdio.h>
int main(void)
{
int sum;
/* COMPUTE RESULT */
sum = 87 - 15;
/* DISPLAY RESULTS */
printf ("The answer is %i \n", sum);
return 0;
}
This C program calculates the result of subtracting 15 from 87 and then displays the result as "The answer is 72" using the `printf` function.
#include <stdio.h>
int main (Void)
(
INT sum;
/* COMPUTE RESULT
sum = 25 + 37 – 19
/* DISPLAY RESULTS //
printf ("The answer is %i\n" sum);
return 0;
}
- Void should be lowercase void.
- Parentheses should be used instead of curly braces for the function body.
- INT should be lowercase int.
- The calculation statement is missing a semicolon at the end.
- The comment delimiter // is incorrect; it should be /* and */.
- There is a missing comma , between the string and variable in the printf statement.
int main(void)
{
int sum;
/* COMPUTE RESULT */
sum = 25 + 37 - 19;
/* DISPLAY RESULTS */
printf("The answer is %i\n", sum);
return 0;
}
👉
int main (void)
{
printf ("Testing...");
printf ("....1");
printf ("...2");
printf ("..3");
printf ("\n");
return 0;
}
OUTPUT =>
Testing.......1...2..3
This C program defines a main() function that sequentially prints out the strings "Testing...", "....1", "...2", and "..3" each on a separate line using the printf() function. It concludes with a newline character. The program then returns 0, indicating successful execution to the operating system.
5.Which of the following are invalid constants? Why?
int main (void)
{
char c, d;
c = 'd';
d = c;
printf ("d = %c\n", d);
return 0;
}
int main()
/* Input length in meters*/
printf("Enter length in meters: ");
scanf("%lf", &meters);
/* Convert meters to centimeters*/
centimeters = meters * 100;
/* Display the result*/
printf("%.2f meters is equal to %.2f centimeters.\n", meters, centimeters);
return 0;
}
#define PI 3.14159
int main()
float radius, height, volume;
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
volume = PI * radius * radius * height;
/* Display the volume*/
printf("The volume of the cylinder is: %.2f cubic units\n", volume);
return 0;
}
This program prompts the user to enter the radius and height of the cylinder, then calculates and displays the volume. Make sure to save the file with a .c extension (e.g., cylinder_volume.c) before compiling and running it.
#include <stdio.h>
int main() {
float subject1, subject2, subject3, subject4, average;
/* Input marks of each subject from the user*/
printf("Enter marks of subject 1: ");
scanf("%f", &subject1);
printf("Enter marks of subject 2: ");
scanf("%f", &subject2);
printf("Enter marks of subject 3: ");
scanf("%f", &subject3);
printf("Enter marks of subject 4: ");
scanf("%f", &subject4);
/* Calculate average marks*/
average = (subject1 + subject2 + subject3 + subject4) / 4;
/* Display the average marks*/
printf("The average marks of the four subjects is: %.2f\n", average);
}
This program prompts the user to enter the marks of each of the four subjects separately, calculates the average, and then displays the result. Compile and run this program in a C compiler like Quincy or any other compiler of your choice.
👉
10.Convert the given temperature in Celsius to Fahrenheit. T(°F) = T(°C) × 1.8 + 32
#include <stdio.h>
int main() {
float celsius, fahrenheit;
/* Input temperature in Celsius from the user*/
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
/* Convert Celsius to Fahrenheit*/
fahrenheit = celsius * 1.8 + 32;
printf("%.2f Celsius is equal to %.2f Fahrenheit\n", celsius, fahrenheit);
return 0;
}
This program prompts the user to enter a temperature in Celsius, then converts it to Fahrenheit using the formula T(°F) = T(°C) × 1.8 + 32, and finally displays the result. Compile and run this program in a C compiler.
👉
11.Find the value of y using y = 3.5x+5 at x = 5.23.
#include <stdio.h>
int main() {
float x, y;
/* Given value of x*/
x = 5.23;
y = 3.5 * x + 5;
printf("When x = %.2f, the value of y is %.2f\n", x, y);
return 0;
}
int main()
float unit_price = 10.50;
int num_items = 5;
float cost;
/* Calculate the cost*/
cost = unit_price * num_items;
printf("The cost of %d items is %.2f Rupees\n", num_items, cost);
return 0;
}
int main()
char name[50], gender;
float height, weight, bmi;
/* Input data from user */
printf("Enter name: ");
scanf("%s", name);
printf("Enter height in meters: ");
scanf("%f", &height);
printf("Enter weight in kilograms: ");
scanf("%f", &weight);
printf("Enter gender (M/F): ");
scanf(" %c", &gender);
/* Calculate BMI*/
bmi = weight / (height * height);
/* Display the result*/
printf("\n%s's BMI: %.2f\n", name, bmi);
return 0;
}
int main()
float inches, centimeters;
/*Input length in inches from the user*/
printf("Enter length in inches: ");
scanf("%f", &inches);
/*Convert inches to centimeters*/
centimeters = inches * 2.54;
/* Display the result */
printf("%.2f inches is equal to %.3f centimeters\n", inches, centimeters);
}
#include <stdio.h>
#define PI 3.142
int main()
float distance_rectangular, distance_semi_circles, total_distance;
float length_rectangular = 67.0;
float breadth_rectangular = 21.0;
distance_semi_circles = 2 * PI * (breadth_rectangular/2);
total_distance = distance_rectangular +distance_semi_circles;
printf("The distance of the running track is %.2f meters\n", total_distance);
return 0;
}
The provided code calculates the total distance of a running track composed of a rectangular part and two semicircular ends. It first initializes variables for the length and breadth of the rectangular part. Then, it computes the distances of the rectangular part and the semicircular ends separately. The distance of the rectangular part is calculated by doubling the length, representing the perimeter of the rectangle. The distance of the semicircular ends is calculated using the formula for the circumference of a circle, where the radius is half the breadth of the track. Finally, the total distance is obtained by summing the distances of the rectangular part and the semicircular ends. The result is printed out with two decimal places, indicating the total distance of the running track in meters.
Comments
Post a Comment