answersLogoWhite

0


Best Answer

#include
#include

bool is_prime(unsigned num) {
unsigned max, factor;
if (num<2) return false;
if (!(num%2)) return num==2;
max = (unsigned) sqrt((double)num) + 1.0;
for (factor=3; factorif (!(num%factor)) return false;
return true;
}

int main() {

unsigned num;

for (num=1; num<=200; ++num)

if (is_prime(num)) printf ("%u is prime\n", num);

return 0;

}

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Implement these methods in JAVA, perhaps with minor changes to primes():

public static void primes() {

for(int i = START; i <= END; i++) {

if(prime(i))

System.out.print(i + ", ");

}

System.out.println();

}

public static boolean prime(int n) {

if(n < 2) return false;

if(n 0) return false;

return true;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

How do Iwrite a Program that prints all prime numbers from an array?

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

You can loop through the array. In Java, this might look like this:

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

System.out.println(myArray[i]);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program to read a value and display all prime numbers up to the value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a program using recursion which should take two values and display 1st value raised to the power of second value?

Write a program using recursion which should take two values and display 1st value raised to the power of second value.


Write a C program to accept a string from user and display its ascii value and then display sum of all ascii value of strings?

//C program to accept a string from user and //display its ascii value and //then display sum of all ascii value of strings #include&lt;stdio.h&gt; #include &lt;string.h&gt; int main() { char String[100]; int Sum,Index; Sum=0; //Sum is initially zero printf("Enter the string:\n"); gets(String); //Accept String from User for(Index=0;Index&lt;strlen(String);Index++) { Sum+=(String[Index]); //Adds (the ASCII values of) the String characters. } printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value. return 0; }


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


Programme to write positive numbers until the user enters no and then sum the numbers divisible by 4 that lie between the range of 10 and 50 and finally display the count and the average value?

ALGORITHM SAMPLE i = 0 REPEAT OUTPUT ("Enter a number: ") INPUT (number[i]) i ++ UNTIL (number[i] 0) THEN counter++ sum = sum + number[i] END IF END FOR DISPLAY (counter) DISPLAY (sum / counter) END SAMPLE


Write the full program for the quadratic equation in COBOL?

Identification division. Program-id. Quadratic. Environment division. Data division. Working-storage section. 01 a pic 9(3) value 0. 01 b pic 9(3) value 0. 01 c pic 9(3) value 0. 01 d pic 9(3) value 0. 01 e pic 9(3) value 0. 01 f pic 9(3) value 0. 01 g pic 9(3) value 0. 01 h pic 9(3) value 0. 01 x1 pic 9(3) value 0. 01 x pic z(3).z(2) value 0. 01 x2 pic 9(3) value 0. 01 y pic z(3).z(2) value 0. Procedure division. Display "Written by Martin O. Egua, but not complete". Display "Quadratic equation solver for three values a, b &amp; c" Display "Enter a number: " Accept a. Display "Enter the second number: " Accept b. Display "Enter the last number: " Accept c. compute d = (b * b) compute e = 4 * a * c compute f = 2 * a compute g = d - e compute h = function sqrt (g). compute x1 = (-b) + h compute x = x1 / f Display "X = " x compute x2 = (-b) - h compute y = x2 / f Display "Y = " y Display "Send the accurate program". Stop run.

Related questions

What are all the negative prime numbers?

A negative number is prime if and only if its absolute value is prime.


Write a program using recursion which should take two values and display 1st value raised to the power of second value?

Write a program using recursion which should take two values and display 1st value raised to the power of second value.


Are negative Numbers prime or Composite?

Negative numbers can be classified as either prime or composite because they still have their absolute value


What 2 prime numbers is ay between?

The answer will depend on the value of ay.


How do you write a program to print prime numbers from 1 to 50 in PHP?

The following PHP Code will display the prime numbers between 2 and 50.You know that 1 is not a prime number so edit the value for "first for loop" according to your requirements (you can also do it for 2 to 1000 or from 900 to 929)Output will be:2is a prime number3is a prime number5is a prime number7is a prime number11is a prime number13is a prime number17is a prime number19is a prime number23is a prime number29is a prime number31is a prime number37is a prime number41is a prime number43is a prime number47is a prime number2013-02-22: Replaced $i/2 with sqrt($i) for efficiency by Ryankirgan


What two prime numbers add up to W?

The answer will depends on the value of W.


Write a cobol program to find prime number or not?

Identification division. Program-id. Prime. Environment division. Data division. Working-storage section. 77 n pic 9(3). 77 q pic 9(3). 77 r pic 9(3). 77 i pic 9(3) value 1. Procedure division. Para-a. Display ( 1 , 1 ) erase. Display ( 2 , 1 ) "enter an integer:". Accept ( 2 , 20 ) n. If n = 1 display ( 3 , 1 ) "number is not prime" go to stop-para. Para-b. Add 1 to i. If i = n display ( 3 , 1 ) "number is prime" go to stop-para. Divide n into i giving q remainder r. If r = 0 display ( 3 , 1 ) "number is not prime" go to stop-para. Go to para-b. Stop-para. Stop run.


What is the ones place value of the product of the first 51 prime numbers?

It is 0. Two of the first 51 prime numbers are 2 and 5, whose product is 10. When you multiply 10 by any other whole numbers, the final digit (in the ones place value) will be 0.


What prime numbers add up to C on a 100 grid?

The answer depends on what value C has.


What are the prime factors of 115?

Prime factorization of 115 is: 5x23 Prime factors are the prime numbers multiplied with each other that together form the original numerical value. Prime numbers are numbers that have no factors besides one and itself. The first prime number is 2, then 3, 5, 7, 11, 13, and so on. Sometimes one may have a number that has multiple same prime numbers for its prime factorization; for example the prime factorization of 256 is 2x2x2x2x2x2x2x2


What is the code of a c program that will read in a positive integer value and determine If the integer is a prime number and If the integer is a Fibonacci number?

see the program


Functions of Min Max and Sum in Microsoft Excel?

Min gets the lowest of a range of values. Max gets the highest value. Sum adds numbers together. So if you had numbers in all the cells from A1 to A20 then: =MIN(A1:A20) will display the lowest value out of all the numbers. =MAX(A1:A20) will display the highest value out of all the numbers. =SUM(A1:A20) will give the total of all the numbers added together.