用指针数组的方法编写一个程序,输入月份号,输出月份的英文名
时间: 2023-05-11 09:06:03 浏览: 174
好的,让我来回答你的问题。以下是用指针数组的方法编写一个程序,输入月份号,输出月份的英文名的代码:
```c
#include <stdio.h>
int main() {
char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
if (month < 1 || month > 12) {
printf("Invalid month number!\n");
return 1;
}
printf("Month name: %s\n", months[month - 1]);
return 0;
}
```
如果你输入的是一个有效的月份号,程序将输出相应的英文月份名。如果你输入的是一个无效的月份号,程序将输出一个错误消息。
希望这个程序能够帮到你!
相关问题
C++用指针数组的方法编写一个程序,输入月份号,输出该月的英文名
当然可以,以下是代码:
```c
#include <stdio.h>
int main() {
char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int month;
printf("请输入月份号:");
scanf("%d", &month);
if (month >= 1 && month <= 12) {
printf("%s\n", months[month - 1]);
} else {
printf("输入的月份号不合法!\n");
}
return 0;
}
```
输入月份号后,程序会输出对应的英文月份名。
用c语言编写一程序,输入月份号,输出该月的英文月名,要求用指针数组处理。
好的,我可以回答这个问题。以下是示例代码:
```c
#include <stdio.h>
int main() {
char *months[] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
int month_number;
printf("Enter month number: ");
scanf("%d", &month_number);
if (month_number < 1 || month_number > 12) {
printf("Invalid month number.\n");
} else {
printf("Month name: %s\n", months[month_number - 1]);
}
return 0;
}
```
该程序先定义了一个指针数组,包含了12个月份的英文名称。然后通过 `scanf` 函数输入月份号,通过条件语句判断月份号是否合法,最后输出对应月份的英文名称。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)