Write a
One way to solve this problem is to use a doubly nested loop. The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n 21 and if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n 21 evenly divides n, then n must be prime. (Note that there are several easy ways to make this
Trending nowThis is a popular solution!
Chapter 3 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Modern Database Management
SURVEY OF OPERATING SYSTEMS
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Electric Circuits. (11th Edition)
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
- Which for statement repeats for all positive four-digit integers in decreasing order? I.e.: the for loop will repeat for 9999, 9998, 9997, ..., 1000. Select one: a. for (int j = 9999; j > 1000; j--) b. for (int j = 1000; j 999; j--) e. None of the othersarrow_forwardWrite a program that asks the user to enter an integer number and checks whether the number is prime or not. Hint: A prime number (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 . . . ) is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number N is prime, divide it in a loop from 2 to N-1 for remainder. If remainder is never 0, then N is prime. For example, 23 and 47 are prime, but 12 and 27 are not.arrow_forwardWrite in JAVAarrow_forward
- JAVA Prime numbers are numbers that have only 2 factors: 1 and themselves. For example, the first 5 prime numbers are 2, 3, 5, 7, and 11. Write a Java program that takes integer inputs N (where N >= 2 ) from the user until the user input is 0 (zero is the sentinel value) and check if the user input value is a prime number or not. Use nested loops. Sample Input and output: Enter a number > 2 Output: prime Enter a number > 11 Output: prime Enter a number > 10 Output: not prime Enter a number > 9 Output: not prime Enter a number > 23 Output: prime Enter a number > 0 Output: Loop ends. Good Bye!arrow_forwardJAVA A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divide x evenly. Take an integer n as input from the keyboard, print true if n is a perfect number, otherwise, print false. You do not need to print any explanation, just print true or false. Use a for loop to generate the divisors. Example 1: Input: num = 28 Output: true Explanation: 28 = 1 + 2 + 4 + 7 + 14 1, 2, 4, 7, and 14 are all divisors of 28. Example 2: Input: num = 6 Output: true Example 3: Input: num = 496 Output: true Example 4: Input: num = 8128 Output: true Example 5: Input: num = 2 Output: falsearrow_forwardWrite a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 40:20 10 5 2 1 Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 40, then with userNum = 2, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks".Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message.arrow_forward
- Write a while loop that prints the multiples of 3 from 300 down to 3. The programming language is java.arrow_forwardWrite a program that reads in numbers from the user and outputs the information specified below. Part One: Get Valid Input Read in two integers from the user: a lower end of a range and an upper end of a range. Check if the numbers are valid (lower cannot be greater than upper). If the numbers are invalid, use a loop to ask for new numbers. Continue looping until you get two valid values. Part Two: Calculate the Sum Use a loop to calculate the sum of all values from lower (inclusive) to upper (inclusive). Output the sum to the user. Part Three: Analyze the Sum Determine if the sum is even or odd. Determine if the sum is positive, negative, or zero. Output this information to the user. Part Four: Calculate the Average Calculate the average of all numbers in the range from lower (inclusive) to upper (inclusive). Style and Additional Coding Requirements code should compile properly space and indent your code follow Java naming conventions for variables (lower camel case with no…arrow_forwardJavaarrow_forward
- Rewrite the following for loop as a while loop. java for(int sum = 0, count = 0; count < 50; count = count + 1, sum = sum + count);arrow_forwardFor Loop Code in Java (more details in the pic below)arrow_forwardUse nested loops to draw a Christmas tree shape of numbers.Sample Run: The Christmas shaped tree for an input of x =5 is: 11 2112 321123 43211234 5432112345 11 11 11 11arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT