Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class AddOrMultiply {
- public static void main(String []args) {
- enterNumber();
- }
- public static void enterNumber() {
- int userNumber;
- char userChar;
- Scanner obj = new Scanner(System.in);
- do {
- System.out.print("Enter an integer number: ");
- userNumber = obj.nextInt();
- if (userNumber == 0) {
- break;
- } else {
- System.out.print("Enter either \'a\' or \'m\' ");
- userChar = obj.next().charAt(0);
- while (userChar != 'a' && userChar != 'm') {
- System.out.println("Invalid choice made.");
- System.out.print("Enter either \'a\' or \'m\' ");
- userChar = obj.next().charAt(0);
- }
- if (userChar == 'a') {
- System.out.printf("The sum is %,d\n", getSum(userNumber));
- } else {
- System.out.printf("The product is %,d\n", getProduct(userNumber));
- }
- }
- } while (userNumber > 0);
- }
- public static long getSum(int input) {
- if (input > 1) {
- return input + getSum(input - 1);
- } else {
- return 1;
- }
- }
- public static long getProduct(int input) {
- if (input >= 1) {
- return input * getProduct(input - 1);
- } else {
- return 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement