Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Methods {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Choose an option from below then enter in number format. ~^_^~");
- System.out.println("1. Factorial");
- System.out.println("2. Sum of Digits");
- System.out.println("3. All Digits Odd");
- System.out.println("4. Average Digit in An Integer");
- System.out.println("5. Midpoint of Three Integers");
- int menuChoice = scan.nextInt();
- System.out.println("Enter an integer.");
- int myInt = scan.nextInt();
- switch (menuChoice) {
- case 1:
- System.out.println(factorial(myInt));
- break;
- case 2:
- System.out.println(sumOfDigits(myInt));
- break;
- case 3:
- System.out.println("something here");
- break;
- case 4:
- System.out.println("something here again");
- break;
- default:
- System.out.println("Choose only between 1 -4.");
- break;
- }
- }
- public static int factorial(int n) {
- int factorial = 1;
- if (n >= 0) {
- for (int i = 1; i <= n; i++) {
- factorial *= i;
- }
- }
- return factorial;
- }
- public static int sumOfDigits(int n) {
- int sum = 0;
- while (n != 0) {
- sum = sum + n % 10;
- n = n / 10;
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment