Q2.1

Write a program that given any two positive integer numbers:
(i) Determines which is the smaller number and which is the
bigger number
(ii) Determines if the numbers are both odd, both even or one
is odd and one is even.
(iii) Prints out all of the numbers between the smaller and
larger number (including the smaller and larger number).

package J2017;

import java.util.*;

public class Q21 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        System.out.println(compare(num1, num2));
    }

    public static int compare(int num1, int num2) {
        int bigger = 0;
        if (num1 > num2) {
            bigger = num1;
        } else {
            bigger = num2;
        }
        return bigger;
    }

    private static void question2(int num1, int num2) {
        if (num1 % 2 == 0 && num2 % 2 == 0) {
            System.out.println("even");
        } else if (num1 % 2 != 0 && num2 % 2 != 0) {
            System.out.println("odd");
        } else {
            System.out.println("odd and even");
        }
    }
}

Q2.2

Write a program that asks the user to enter a person’s first
name, middle name and last name. The program should then
print the names in the following format:
Last name, First name, M.
where M is the first letter of their middle name.
The program should always print the first letter of each name
in uppercase and the remaining letters in lowercase
irrespective of the case the name was entered in.
A sample run of the program could be as follows:

Please enter your first name:
John
Please enter your middle name:
Tom
Please enter your last name:
Bloggs
Name is: Bloggs, John, T.

package J2017;

import java.util.*;

public class Q22 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your first name:");
        String fName = input.next();
        System.out.println("Please enter your middle name:");
        String mName = input.next();
        System.out.println("Please enter your last name:");
        String lName = input.next();
        System.out.println(printName(fName, mName, lName));
    }

    private static String printName(String fName, String mName, String lName) {
        String str = "";
        str += lName + "," + fName + "," + mName.charAt(0) + ".";
        return str;
    }

}

Q2.3

Write a program that given a positive integer number:
(i) Determines the largest number other than the number itself
that divides evenly into the number
(ii) Determines and displays a count of the total number of
numbers that divide evenly into the number (excluding the
number itself).

For example, given the number 12, the program should display:
The largest number that divides evenly into 12
excluding itself is 6.
5 numbers divide evenly into 12, excluding
itself.

package J2017;

public class Q23 {
    public static void main(String[] args) {
        int num = 234;
        int sum = 0;
        boolean flag = false;
        for (int i = num - 1; i > 0; i--) {
            if (num % i == 0) {
                sum++;
                if (flag == false) {
                    System.out.println(
                            "The largest number that divides evenly into " + num + " excluding itself is " + i);
                }
                flag = true;
            }
        }
        System.out.println(sum + " numbers divide evenly into " + num + ", excluding itself.");
    }

}
最后修改日期:2019年12月31日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。