< 재귀 함수 사용>
#include <iostream.h>
int fibona(int a);
void main()
{
int count, result;
cout<<"Enter the number: ";
cin>>count;
if(count < 0)
cout<<"error"<<endl;
else
{
result = fibona(count);
cout<<"The result is : "<<result<<endl;
}
}
int fibona(int a)
{
if ( a == 1) return 1;
if ( a == 2) return 1;
return fibona(a-2) + fibona(a-1);
}
============================================================================================================
< 반복문 사용>
#include <iostream.h>
void main()
{
int result, i,count;
int arr[100];
cout<<"Enter the number: ";
cin>>count;
for(i=0; i<2; i++)
{
arr[i] = 1;
}
for( i =0; i< count; i++)
{
arr[i+2] = arr[i] + arr[i+1];
cout<< arr[i] <<endl;
}
cout<<"The result is : "<< arr[count-1] <<endl;
}
'Education > C++ programming' 카테고리의 다른 글
Compiler (0) | 2012.01.13 |
---|---|
Variable (1) | 2012.01.13 |
리눅스 c++ 컴파일 (2) | 2009.11.24 |
Triangle & Rectangle program code (0) | 2009.11.24 |