Question

Part A Write a Java program that reads an integer n from the keyboard and loops...

Part A

Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised.

Part B

Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.  

Will give thumbs up to best answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
a)
import java.util.Scanner;

public class KeepReadingUntilValid {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n;
        do {
            n = in.nextInt();
        } while (n < -13 || n > 13);
    }

}


b)
import java.util.Scanner;

public class FactorialOfValid {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n;
        do {
            n = in.nextInt();
        } while (n < 0 || n > 13);

        int f = 1;
        for(int i = 1; i <= n; ++i) {
            f *= i;
        }
        System.out.println(f);
    }

}
Add a comment
Know the answer?
Add Answer to:
Part A Write a Java program that reads an integer n from the keyboard and loops...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT