C语言斐波那锲数列前n项求和

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43959027/article/details/89636728
#include"stdio.h"
void main()
{
	int n = 20;
	int sum = 0;
	int temp1 = 1, temp2 = 1,temp;
	printf("请输入项数:");
	scanf("%d", &n);
	if(n == 1)
	{
		sum += temp1;
	}
	else if(n == 2)
	{
		sum += temp1 + temp2;
	}
	else
	{
		sum = 2;
		temp = temp1 + temp2;
		for (int i = 3; i <= n; i++)
		{
			sum += temp;
			temp1 = temp2;
			temp2 = temp;
			temp = temp1 + temp2;
		}
	}
	printf("%d\n",sum);
}

猜你喜欢

转载自blog.csdn.net/qq_43959027/article/details/89636728