Ex17-팩토리얼 출력 (Factorial)

CODEDRAGON Development/C, C++

반응형

   

팩토리얼 출력

5!

5*4*3*2*1

5*4!

4!

4*3*2*1

4*3!

3!

3*2*1

3*2!

2!

2*1!

2*1!

1!

1

  

n!

n*(n-1)!

  


   

팩토리얼

http://codedragon.tistory.com/3320

   


소스코드

   

  1. #include <stdio.h>
  2.  
  3. // 함수의 선언(출력O입력O 형태)
  4. int factorial(int n);
  5.  
  6. int main(void) {
  7.     int f;
  8.     int result;        // 팩토리얼 계산 결과를 저장할 변수
  9.  
  10.     printf("정수 입력 > " );
  11.     fflush(stdout);
  12.     scanf("%d", &f);
  13.  
  14.     result=factorial(f);            // 함수의 호출
  15.     printf( "%d 팩토리얼은 %d입니다. \n", f, result);
  16.     return 0;
  17. }
  18.  
  19. // 함수의 정의
  20. int factorial(int n) {
  21.     if (n<=1)
  22.         return 1;    //1!=1이므로 1 리턴
  23.  
  24.     else
  25.         return n * factorial(n-1);
  26. }


   

   

출력결과

 

반응형

'Development > C, C++' 카테고리의 다른 글

배열 선언 시 주의할 점  (0) 2015.07.14
* 연산자, *&연산자 - 메모리 구조(총정리)  (0) 2015.07.14
LAB03-윤년 계산기  (0) 2015.07.13
LAB02-동전교환기  (0) 2015.07.13
LAB01-산술연산 계산기  (0) 2015.07.13