2013년 8월 13일 화요일

[ORACLEJAVA커뮤니티]자바 팩토리얼 예제

import java.io.*;                   // 입력받기위해 io관련 클래스 import
public class Pactorial{
public static void main(String [] args){
BufferedReader in;
             String a=null;
             int b, j;
             int sum;
            
             System.out.println("숫자를 입력하세요");
:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 
             try                             // 키보드로 입력받기 위해 예외처리 해줌
             {
                in=new BufferedReader (new InputStreamReader(System.in));
                a=in.readLine();
                                                    
             }catch (java.io.IOException ex){
               System.out.println(ex);
             }
            
             b = Integer.parseInt(a);                     //String int형으로 변환
        System.out.println("입력받은 숫자는 : "+b);  // 입력받은 숫자를 출력
 
             for(int i=b;i>0;i--){                   // 입력받은 숫자부터 1까지 Pactory
sum=i;
               for(j=i-1;j>0;j--){                        // 자기 자신부터 1이 될때까지
                 sum=sum*j;                            // 계속 곱해줌
               }
               System.out.println(i+"! : "+ sum);         // Patorial계산해준값 출력
             }
}
}
 
:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /> 
 
[결과]
8
입력받은 숫자는 : 8
8! = 40320
7! = 5040
.
.
.
.
1! = 1

댓글 없음:

댓글 쓰기