Question

Implement a nested for loop to jind the prime number up to n numbers. print all...

Implement a nested for loop to jind the prime number up to n numbers. print all the prime numbers up to N, we start one loop from 2 to N and then inside the loop we check current number or “num” is prime or not. To check if it is prime or not we again need one nested loop. It is not an efficient way to check prime number but it is simpler to understand the basic of looping in Java

Homework Answers

Answer #1
class printPrime 
{ 
    static void printPrime(int n) 
    { 
        for(int i=2;i<=n;i++)
        {
            int count=0;
            for(int j=1;j<=i;j++)
            {
                    if(i%j==0)            //if the number is divisible by j
                    count++;
            }
            if(count==2)       //checking if the number is prime
            System.out.print(i+" ");
        }
    } 

    public static void main(String[] args) 
    { 
        int n = 10; 
        printPrime(n); 
    } 
} 

for any doubt just leave a comment else please press the like button

Know the answer?
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
Similar Questions
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop to complete the task. A while loop has this basic structure: /* variable initializations */ while (/*stop condition*/){ /* statements to be performed multiple times */ /* make sure the variable that the stop condition relies on is changed inside the loop. */ } Despite the structure of the while loop being different than that of a for loop, the concept behind it is...
[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can...
[PART ONE OF PROJECT, ALREADY COMPLETED] An accumulator is a primitive kind of calculator that can evaluate arithmetic expressions. In fact, the Arithmetic-Logic Unit (ALU) of the rst computers was just an accumulator. An arithmetic expression, as you know, consists of two kinds of tokens: operands and operators All our operands will be (float) numbers and for a start, we shall use only two operators: + (plus) and - (minus) A sample run of the program would look like this....
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
This must be answered not advance methods, focusing on String method. We are working on Ch...
This must be answered not advance methods, focusing on String method. We are working on Ch 9 in Think Java 1st Ed.I need the program to be bare bones and the coding need to be done the long way with no advanced code. in this lab you will have two methods with headings: - public static int countNumberSigns(String tweetText) - public static int countHashtags(String tweetText) 'String tweetText' means the method is expectiong a string value as an input to it....
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT