Question 01
Write a C program that calculates the force based on Newton’s second law of motion. The program should prompt the user to enter the mass of an object (in kilograms) and its acceleration (in meters per second squared). It should then compute the force using the formula: F = m × a. Assume the numbers can contain non-integers. Use the following help.
// Define a decimal number (floating-point number) e.g. 9.8
float num;
// Read a floating-point number
scanf("%f", &num);
// Write the result
printf("%f");
// Write the result with a precision of 2 digits after
// after the floating point
printf("%.2f");
Answer 01
#include <stdio.h>
void main()
{
int mass, acceleration, force;
// Ask the user for the mass of the object
printf("Enter the mass of the object (in kg): ");
scanf("%f", &mass);
// Ask the user for the acceleration
printf("Enter the acceleration (in m/s^2): ");
scanf("%f", &acceleration);
// Calculate the force using F = m * a
force = mass * acceleration;
// Output the result
printf("The force on the object is: %.2f newtons\n", force);
}
Question 02
Write a C program that calculates the kinetic energy of an object based on the formula: KE=(1/2)mv2.
Answer 02
#include <stdio.h>
void main()
{
float mass, velocity, kinetic_energy;
// Ask the user for the mass of the object
printf("Enter the mass of the object (in kg): ");
scanf("%f", &mass);
// Ask the user for the velocity of the object
printf("Enter the velocity of the object (in m/s): ");
scanf("%f", &velocity);
// Calculate the kinetic energy using KE = 0.5 * m * v^2
kinetic_energy = 0.5 * mass * velocity * velocity;
// Output the result
printf("The kinetic energy of the object is: %.2f joules\n", kinetic_energy);
}
Question 03
Write a C program that asks the user to input the temperature of water (in degrees Celsius) and determines its physical state: solid, liquid, or gas.
-
Water is solid if the temperature is less than or equal to 0oC.
-
Water is liquid if the temperature is between 0oC and 100oC (exclusive).
-
Water is gas if the temperature is greater than or equal to 100oC.
The program should then output the state of the water. Use the following help.
// If A is less than or equal to B
A <= B
// If A is greater than or equal to B
A >= B
// If condition1 AND condition2
if(condition1 && condition2)
{...}
Answer 03
#include <stdio.h>
void main()
{
float temperature;
// Ask the user to enter the temperature
printf("Enter the temperature of water (in Celsius): ");
scanf("%f", &temperature);
// Determine the state of water using if statements
if (temperature <= 0)
{
printf("The water is in a solid state (ice).\n");
}
else if (temperature > 0 && temperature < 100)
{
printf("The water is in a liquid state.\n");
}
else if (temperature >= 100)
{
printf("The water is in a gaseous state (steam).\n");
}
}
Question 04
Write a C program that calculates the Body Mass Index (BMI) of a person. The program should ask the user to enter their weight in kilograms and height in meters. It then calculates the BMI using the formula: BMI=weight/height2. Based on the BMI value, the program should classify the person as:
-
Underweight if BMI is less than 18.5,
-
Normal weight if BMI is between 18.5 and 24.9,
-
Overweight if BMI is between 25 and 29.9,
-
Obese if BMI is 30 or higher.
Answer 04
#include <stdio.h>
void main()
{
float weight, height, bmi;
// Ask the user to enter weight and height
printf("Enter your weight (in kg): ");
scanf("%f", &weight);
printf("Enter your height (in meters): ");
scanf("%f", &height);
// Calculate BMI
bmi = weight / (height * height);
// Output the calculated BMI
printf("Your BMI is: %.2f\n", bmi);
// Classify the BMI using if statements
if (bmi < 18.5)
{
printf("You are classified as Underweight.\n");
}
else if (bmi >= 18.5 && bmi <= 24.9)
{
printf("You are classified as Normal weight.\n");
}
else if (bmi >= 25 && bmi <= 29.9)
{
printf("You are classified as Overweight.\n");
}
else
{
printf("You are classified as Obese.\n");
}
}