Question

Task 5.2 Numerical Analysis Using Nested Loops (13 pts) Consider the following program: void setup() {...

Task 5.2 Numerical Analysis Using Nested Loops (13 pts)

Consider the following program:

void setup()
{

//Read a positive integer from the user

//Your code starts here }

Complete this program such that it calculates all prime numbers between 1 and the value that is assigned to variable num and outputs them on the screen. A prime number is a positive integer that has no other factors other than itself and 1. You should use a nested loop, i.e., write code where a for loop runs within another for loop. DO NOT USE CLASSES/OBJECTS, METHODS, OR APPLETS; simply use a nested loop.

The program should print prime numbers in reverse order, i.e., starting with the largest one.

Example: When num = 10, the program should print: 7 5 3 2 , which are the prime numbers between 1 and 10.

Example: When num = 19, the program should print 17 13 11 7 5 3 , which are the prime numbers between 1 and 19.

HINT: If you are having problems with this task, start by writing a for loop to check if a number is prime or not. Once this is complete, nest this loop within another loop to check all numbers in the range given by the user.

___________________________________________________________________________________________________________________________________________________

This is the code that I've written in attempting this problem. Please modify it and make any appropriate changes. The idea behind my code is that since the numbers have to be printed in reverse order, I've initialized i in the outer loop to equal num and then I have a decrementing expression so that it can detect, store, and then print all the prime numbers between 1 and num in reverse order. Note: I am very new to processing (I only started learning it 3 weeks ago, so please don't go beyond the scope of what I've learnt (which is strings/chars, if/else decision structures, and loops).

void setup(){
import javax.swing.JOptionPane;
int num;
num=parseInt(JOptionPane.showInputDialog("Enter a positive, real integer"));
int i;
int j;

for (i = num; i > 1; i--){
for (j = 2; j < i; j++){
if (i%j == 0)
{
break;
}
}
if (i==j)
{
print(num);
}
}
println();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// PrimeNos.java

import java.util.Scanner;

public class PrimeNos {

   public static void main(String[] args) {
setUp();

   }

   private static void setUp() {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
int num;
while(true)
{
System.out.print("Enter Number :");
num=sc.nextInt();
if(num>0)
{
break;
}
else
{
System.out.println("** Invalid.Must be positive number **");
}
}
  
int i=num;
for(i=num;i>0;i--)
{
int flag = 0;
int j = 2;
for(j=2;j < i;j++)
{
if (i % j == 0)
{
flag = 1;
}
}
if (flag == 0 && i!=1)
{
System.out.print(i+" ");
}
  
}

      
   }

}
________________________

Output:

Enter Number :50
47 43 41 37 31 29 23 19 17 13 11 7 5 3 2

________________________Thank You

Add a comment
Know the answer?
Add Answer to:
Task 5.2 Numerical Analysis Using Nested Loops (13 pts) Consider the following program: void setup() {...
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
  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • AT46.1: Nested loops: Indent text HALLENGE 461: Nested loops: Indent text Print numbers 0,1,2 userNum as...

    AT46.1: Nested loops: Indent text HALLENGE 461: Nested loops: Indent text Print numbers 0,1,2 userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex userNum-3 prints 3 int noin(void){ 6 int 1- Your solution goes here 1e return...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • MATLAB Write segment of code using nested for loops to print out the following pattern. Your...

    MATLAB Write segment of code using nested for loops to print out the following pattern. Your segments of code should prompt the user for a positive integer, n, and print the following star pattern depending on n. An equilateral triangle. Pictured below is case n = 6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *...

  • Create a program using the Random class and While loops in Java. The program should generate...

    Create a program using the Random class and While loops in Java. The program should generate a random number from 50 through 100. The loop should add the five random numbers generated. On each iteration of the loop the program should print out the number generated, how many numbers have been generated thus far, and the sum so far. On the last iteration, the printout should say the The final total is.... Teach comment the last time I did it:...

  • import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[]...

    import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice {       private static final int[] arr = new int[100000];    public static void main(String[] args) {        for(int i=0; i<100000; i++)            arr[i] = i;               //TODO comment out this line        LinkedList<Integer> list = new LinkedList<Integer>();               //TODO uncomment this line        //List<Integer> list = new ArrayList<Integer>();               //TODO change the rest of the...

  • For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum...

    For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Challenge 4.6.1: Nested loops ndent...

  • Not sure how to get rid of the spaces at the end. Thanks for any help...

    Not sure how to get rid of the spaces at the end. Thanks for any help learn.zybooks.com 21.7. Nested loops zyBooks My library EGR 219 home 21.7: Nested loops E zyBooks catalog Help/FAQ Dennis Roberts CAHALE.7.1: Nested loops: Indent text Print numbers 0, 1,2,. serNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

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