fig06_28.cpp

fig06_28.cpp

#include<iostream>
#include<iomanip>
using namespace std;

unsigned long factorial(unsigned long);
int main()
{
	for (unsigned int counter = 1; counter <= 10; ++counter)
		cout << setw(2) << counter << "!= " << factorial(counter)
		<< endl;

	system("pause");
	return 0;
}

unsigned long factorial(unsigned long number)
{
	if (number <= 1)
		return 1;
	else
		return number*factorial(number - 1);

}

fig06_29.cpp

#include<iostream>
using namespace std;

unsigned long fibonacci(unsigned long);

int main()
{
	for (unsigned int counter = 0; counter <= 10; ++counter)
		cout << "fibonacci(" << counter << ")="
		<< fibonacci(counter) << endl;

	cout << "\nfibonacci(20) = " << fibonacci(20) << endl;
	cout << "fibonacci(30) = " << fibonacci(30) << endl;
	cout << "fibonacci(35) = " << fibonacci(35) << endl;

	system("pause");
	return 0;
}

unsigned long fibonacci(unsigned long number)
{
	if ((0 == number) || (1 == number))
		return number;
	else
		return fibonacci(number - 1) + fibonacci(number - 2);
}

fig06_21.cpp

#include<iostream>
#include<iomanip>
#include<array>

using namespace std;

int main()
{
	array<int, 5> n;

	for (size_t i = 0; i < n.size(); ++i)
		n[i] = 0;

	cout << "Element" << setw(13) << "Value" << endl;

	for (size_t j = 0; j < n.size(); ++j)
		cout << setw(7) << j << setw(13) << n[j] << endl;

	system("pause");
	return 0;
}


fig7.4.cpp


#include<iostream>
#include<iomanip>
#include<array>
using namespace std;

int main()
{
	array<int, 5> n = { 32, 27, 64, 18, 95 };

	cout << "Element" << setw(13) << "Value" << endl;

	for (size_t i = 0; i < n.size(); ++i)
		cout << setw(7) << i << setw(13) << n[i] << endl;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值