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