《C++ Primer Plus》学习笔记——第二章 初识C++(三)

本篇进行编程练习

1.编写一个C++程序,它显示您的姓名和喜好。

#include <iostream>

int main ()
{
	using namespace std;

	cout<<"My name is GM_AMRC."<<endl;
	cout<<"I love play computer game."<<endl;
	return 0;
}

显示结果为:

My name is GM_AMRC.
I love play computer game.

2.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月。

#include <iostream>

int main ()
{
	using namespace std;

	int age;
	cout<<"Enter your age:";
	cin>>age;
	cout<<"It has "<<age*12<<" months."<<endl;

	return 0;
}

显示结果为:

Enter your age:29
It has 348 months.

3.编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,以9:10这样的格式显示这两个值。

#include <iostream>

using namespace std;

void Output(int hours,int mins);

int main ()
{
	int hours;
	cout<<"Enter the number of hours:";
	cin>>hours;
	int mins;
	cout<<"Enter the number of minutes:";
	cin>>mins;
	Output(hours,mins);

	return 0;
}

void Output(int hours,int mins)
{
	cout<<"Time: "<<hours<<":"<<mins<<endl;
}

显示结果为:

Enter the number of hours:9
Enter the number of minutes:10
Time: 9:10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值