📅  最后修改于: 2023-12-03 15:01:24.817000             🧑  作者: Mango
本程序用于计算一段时间内(默认为10年)每年的耕地面积。用户需提供初始耕地面积和每年新开垦出的耕地面积。程序会在控制台输出每年的总耕地面积。
程序需要计算每年的总耕地面积,需要使用循环结构。本程序使用for
循环结构。
程序需要接收用户提供的耕地面积信息,使用scanf
函数实现。
程序需要输出每年的总耕地面积信息,使用printf
函数实现。
#include <stdio.h>
int main(){
double initial_area, new_area, area = 0;
int years = 10;
printf("Please enter the initial cultivated area: ");
scanf("%lf", &initial_area);
printf("Please enter the new cultivated area per year: ");
scanf("%lf", &new_area);
for(int i=1; i<=years; i++){
area = initial_area + new_area * i;
printf("The cultivated area in year %d is %.2lf hectares.\n", i, area);
}
return 0;
}
程序测试结果如下:
输入:
Please enter the initial cultivated area: 100
Please enter the new cultivated area per year: 10
输出:
The cultivated area in year 1 is 110.00 hectares.
The cultivated area in year 2 is 120.00 hectares.
The cultivated area in year 3 is 130.00 hectares.
The cultivated area in year 4 is 140.00 hectares.
The cultivated area in year 5 is 150.00 hectares.
The cultivated area in year 6 is 160.00 hectares.
The cultivated area in year 7 is 170.00 hectares.
The cultivated area in year 8 is 180.00 hectares.
The cultivated area in year 9 is 190.00 hectares.
The cultivated area in year 10 is 200.00 hectares.
测试结果符合预期,说明程序实现正确。