HW02 Python運算子表達式
1. Notebooklm
2.
3.學習卡及測驗
1. 專案名稱
car-speed-calculator
2. C 語言程式碼
檔案名稱:main.c
c
#include <stdio.h>
int main() {
float distance, time, speed;
// Input distance in meters
printf("Enter distance in meters: ");
scanf("%f", &distance);
// Input time in seconds
printf("Enter time in seconds: ");
scanf("%f", &time);
// Calculate speed
if (time > 0) {
speed = distance / time;
printf("Speed of the car = %.2f m/s\n", speed);
} else {
printf("Time must be greater than zero.\n");
}
return 0;
}
程式說明
#include <stdio.h>:引入標準輸入與輸出函式庫。float distance, time, speed;:宣告距離、時間和速度變數。scanf():讓使用者透過鍵盤輸入距離和時間。speed = distance / time;:使用距離除以時間計算速度。if (time > 0):確保時間大於零,避免除以零。printf():顯示計算結果。%.2f:將速度顯示至小數點後兩位。
計算公式:
3. GitHub README.md
建立一個名為 README.md 的檔案,並將以下內容貼上。
markdown
# Car Speed Calculator ## Introduction This project is a simple C program that calculates the speed of a car in meters per second (m/s). The distance in meters and time in seconds are entered by the user through the keyboard. ## Formula Speed = Distance / Time ## Features - Accepts distance in meters. - Accepts time in seconds. - Calculates speed in meters per second. - Checks that time is greater than zero. - Displays the result to two decimal places. ## Requirements - A C compiler, such as GCC. - A terminal or command prompt. ## How to Run ### 1. Compile the program ```bash gcc main.c -o car_speed ``` ### 2. Run the program On Windows: ```bash car_speed.exe ``` On Linux or macOS: ```bash ./car_speed ``` ## Example Output ```text Enter distance in meters: 100 Enter time in seconds: 20 Speed of the car = 5.00 m/s ``` ## Program Explanation The program reads the distance and time from the keyboard and calculates the car's speed by dividing the distance by the time. If the time is zero or negative, the program displays an error message. ## Author Your Name ## License This project is for educational purposes.程式 學號 % 7 + 1
5.C Program
c#include <stdio.h> int main() { float distance, time, speed; printf("Enter distance in meters: "); scanf("%f", &distance); printf("Enter time in seconds: "); scanf("%f", &time); speed = distance / time; printf("Speed of the car = %.2f m/s\n", speed); return 0; }Formula
Sample Output
textEnter distance in meters: 100 Enter time in seconds: 20 Speed of the car = 5.00 m/sExplanation: The program reads the distance in meters and time in seconds from the keyboard, then calculates the car's speed in meters per second (m/s).












留言
張貼留言