Advertisement
Guest User

Untitled

a guest
Dec 29th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // 2019/12/28(土)、29(日)
  2. // 階乗
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Fact {
  7. public static void main(String[] args) {
  8. // 1!=1 2!=2×1 3!=3×2×1 …
  9. // 0!は1とする
  10. int factNum;
  11. int result;
  12. Scanner scanner = new Scanner(System.in);
  13. System.out.println("0から10までのいずれかの整数を入力");
  14. while (true) {
  15. factNum = scanner.nextInt();
  16. if (factNum < 0 || factNum > 10) {
  17. System.out.println("0から10までの整数を入力");
  18. } else {
  19. break;
  20. }
  21. }
  22. result = 1;
  23. for (int i = 1; i <= factNum; i++) { // i=0の時、このfor文は実行されない
  24. result *= i;
  25. }
  26. System.out.println(factNum + "の階乗は" + result);
  27. }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement