Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Learning objectives: Write code that prompts the user for input Reading data fro

ID: 3749548 • Letter: L

Question

Learning objectives: Write code that prompts the user for input Reading data from the keyboard with a Scanner variable Using a while loop to allow the user to enter multiple cases Using a loop to perform mathematical calculations . Writing a method . Writing comments What should you submit via Canvas? A file called HWFour.java that contains your Java code and copy your output into the comment window Project Description Your code will . ask the user to enter a positive integer . determine if the positive integer is 1. divisible by 3 2. divisible by 5 . display the results of your calculations Your output should be identical to the following output Case 1: Assume the user enters 100000000000000010020000005 Your code displays the following output You entered 100000000000000010020000005 This number is divisible by 3 and 5

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// HWFour.java

import java.util.Scanner;

public class HWFour {

      public static void main(String[] args) {

            // defining scanner and required variables

            Scanner scanner = new Scanner(System.in);

            String number;

            int sum = 0;

            boolean divisibleBy3, divisibleBy5;

            System.out.print("Enter a positive integer: ");

            // getting number as String

            number = scanner.nextLine();

            // finding last digit

            char lastDigit = number.charAt(number.length() - 1);

            // checking if last digit is 5 or 0

            if (lastDigit == '5' || lastDigit == '0') {

                  divisibleBy5 = true;

            } else {

                  divisibleBy5 = false;

            }

            // going through all characters

            for (int i = 0; i < number.length(); i++) {

                  // converting each character to digit and adding to sum

                  sum += Character.getNumericValue(number.charAt(i));

            }

            // if sum is divisible by 3, then the number is divisible by 3

            if (sum % 3 == 0) {

                  divisibleBy3 = true;

            } else {

                  divisibleBy3 = false;

            }

            // displaying the results based on the values of two boolen variables

            if (divisibleBy3 && divisibleBy5) {

                  System.out.println("The number is divisible by 3 and 5");

            } else if (divisibleBy3) {

                  System.out

                              .println("The number is divible by 3 but not divisible by 5");

            } else if (divisibleBy5) {

                  System.out

                              .println("The number is divible by 5 but not divisible by 3");

            } else {

                  System.out.println("The number is not divible by 3"

                              + " and it is not divisible by 5");

            }

      }

}

/*OUTPUT (multiple runs)*/

Enter a positive integer: 10000000000000000000000000000000111520000000000000005

The number is divible by 5 but not divisible by 3

Enter a positive integer: 300106305999

The number is divible by 3 but not divisible by 5

Enter a positive integer: 1000000010110111

The number is not divible by 3 and it is not divisible by 5

Enter a positive integer: 1500000000000000000000000000000000000

The number is divisible by 3 and 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote