fork download
  1. #include <stdio.h>
  2. //フィボナッチ数列
  3. int rec(int n){
  4.  
  5. if(n==1){
  6. return 1;
  7. }
  8. else if(n==2){
  9. return 1;
  10. }
  11. else{
  12. return rec(n-1)+rec(n-2);
  13. }
  14. }
  15. int main(void) {
  16. int n;
  17. scanf("%d",&n);
  18. int i;
  19. for(i=1;i<=n;i++){
  20. printf("フィボナッチ数列の第%d項は%d\n",i,rec(i));
  21. }
  22. return 0;
  23.  
  24. }
  25.  
  26.  
Success #stdin #stdout 0s 5320KB
stdin
10
stdout
フィボナッチ数列の第1項は1
フィボナッチ数列の第2項は1
フィボナッチ数列の第3項は2
フィボナッチ数列の第4項は3
フィボナッチ数列の第5項は5
フィボナッチ数列の第6項は8
フィボナッチ数列の第7項は13
フィボナッチ数列の第8項は21
フィボナッチ数列の第9項は34
フィボナッチ数列の第10項は55