binibiningtinamoran

Methods

Nov 2nd, 2020 (edited)
3,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Methods {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         System.out.println("Choose an option from below then enter in number format. ~^_^~");
  10.  
  11.  
  12.         System.out.println("1. Factorial");
  13.         System.out.println("2. Sum of Digits");
  14.         System.out.println("3. All Digits Odd");
  15.         System.out.println("4. Average Digit in An Integer");
  16.         System.out.println("5. Midpoint of Three Integers");
  17.  
  18.         int menuChoice = scan.nextInt();
  19.  
  20.         System.out.println("Enter an integer.");
  21.             int myInt = scan.nextInt();
  22.  
  23.         switch (menuChoice) {
  24.             case 1:
  25.                 System.out.println(factorial(myInt));
  26.                 break;
  27.             case 2:
  28.                 System.out.println(sumOfDigits(myInt));
  29.                 break;
  30.             case 3:
  31.                 System.out.println("something here");
  32.                 break;
  33.             case 4:
  34.                 System.out.println("something here again");
  35.                 break;
  36.             default:
  37.                 System.out.println("Choose only between 1 -4.");
  38.                 break;
  39.         }
  40.     }
  41.  
  42.     public static int factorial(int n) {
  43.  
  44.         int factorial = 1;
  45.         if (n >= 0) {
  46.             for (int i = 1; i <= n; i++) {
  47.                 factorial *= i;
  48.             }
  49.         }
  50.         return factorial;
  51.     }
  52.  
  53.     public static int sumOfDigits(int n) {
  54.         int sum = 0;
  55.  
  56.         while (n != 0) {
  57.             sum = sum + n % 10;
  58.             n = n / 10;
  59.         }
  60.  
  61.         return sum;
  62.     }
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment