Write C program code that assigns the correct fee to type int variable Fee based on the value of type int variable Age.
时间: 2024-05-02 20:18:40 浏览: 86
Here's a possible solution:
```c
int age, fee;
// prompt the user to enter their age
printf("Enter your age: ");
scanf("%d", &age);
// assign the fee based on the age
if (age < 18) {
fee = 10;
} else if (age >= 18 && age < 65) {
fee = 20;
} else {
fee = 15;
}
// print the fee
printf("Your fee is $%d\n", fee);
```
In this code, we first declare two variables: `age` and `fee`, both of type `int`. We then prompt the user to enter their age and read the input into the `age` variable using `scanf()`.
Next, we use an `if` statement to determine the appropriate fee based on the age. If the age is less than 18, the fee is set to $10. If the age is between 18 (inclusive) and 65 (exclusive), the fee is set to $20. If the age is 65 or greater, the fee is set to $15.
Finally, we print the fee using `printf()`. The `%d` format specifier is used to print the value of the `fee` variable as an integer. The output will be something like:
```
Enter your age: 25
Your fee is $20
```
阅读全文