JAVA

210806_숫자의승,factorial

요옫 2021. 8. 6. 16:30

(예제)

//x의 y승이라는 것:x를 y의 횟수만큼 곱하는 것

 

Scanner sc=new Scanner(System.in);

int x,y;

 

System.out.println("두 수를 입력하세요");

x=sc.nextInt();

y=sc.nextInt();

 

int result=1;  //곱셈이니까 1

 

for(int i=1;i<=y;i++)

result*=x;

System.out.println(x+"의 "+y+"승은 "+result +"입니다")

 

//결과

 

두 수를 입력하세요

5 4

5의 4승은 625입니다

 

--------------------

 

(예제)

//5의 factorial 구하기 5!=120

 

Scanner sc=new Scanner(System.in);

 

int n=0;

 

System.out.println("팩토리얼 숫자 입력");

n=sc.nextInt();

 

int result=1;  //곱셈이니까 0이 아닌 1

 

for(int i=1;i<=n;i++)

result*=i;  //이때 n이 아니라 i

System.out.println(n+"! = "+result);

 

//결과

팩토리얼 숫자 입력

5

5! = 120

'JAVA' 카테고리의 다른 글

210809_배열 이용하여 숫자,이름 찾기  (0) 2021.08.09
210908_배열  (0) 2021.08.09
210806_startswith,equals  (0) 2021.08.06
210806_예제/for,while  (0) 2021.08.06
210806_random  (0) 2021.08.06