C++ Primer Plus P24 编程题五(编写程序,美(或其他)人口占比全球量)——中职

C++ Primer Plus P24 编程题五

编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在longlong变量中,
使程序输出显示美国(或其他国家)的人口占全世界人口的百分比。该程序的输出应与下面类似:

Enter the world’s population:6898758899
Enter the population of US:310783781
The population of the US is 4.50492% os the world popution.

/*
C++ Primer Plus P24 编程题五

编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在longlong变量中,
使程序输出显示美国(或其他国家)的人口占全世界人口的百分比。该程序的输出应与下面类似:

Enter the world's population:6898758899
Enter the population of US:310783781
The population of the US is 4.50492% os the world popution.
*/

//头文件
#include<iostream>

//主函数
int main(void)
{
    
    
	using namespace std;															//编译指令
	_Longlong world_popution, US_popution;											//在VS2015中longlong被指定为_Longlong
	double percent;									

	cout << "Enter the world's population:";										//显示提醒用户输入世界人口数
	cin >> world_popution;															//记录世界人口
	cout << "Enter the population of US:";											//显示提醒用户输入美国(或其他国家)人口数
	cin >> US_popution;																//记录美国(或其他国家)人口数

	percent = 100 * (double(US_popution) / double(world_popution));					//计算美国(或其他国家)的人口占全世界人口的百分比

	cout << "The population of the US is " << percent << "% os the world popution."	//输出结果
		<< endl;

	return 0;
}

计算美国(或其他国家)的人口占全世界人口的百分比的方法

percent = 100 * (double(US_popution) / double(world_popution));					计算美国(或其他国家)的人口占全世界人口的百分比

结果:

Enter the world's population:6898758899
Enter the population of US:310783781
The population of the US is 4.50492% os the world popution.

感谢观看

再次感谢~

猜你喜欢

转载自blog.csdn.net/qq_51212951/article/details/113590935