answersLogoWhite

0


Best Answer

// returns 1 if n is prime, 0 otherwise

void isPrime(const int n) {

// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]

// so we compute the square root only once to limit our number of calculations.

const int sqrt_n = sqrt(n);

// Iterate through possible factors

int i;

for( i = 2; i <= sqrt_n; ++i ) {

// If n is divisible by i (n%i==0) then we have a factor

if(!(n % i)) {

return 0;

}

}

// If we get here, we know n has no factors other than itself and 1

return 1;

}

User Avatar

Wiki User

βˆ™ 15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 10y ago

OK, done that

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Print 1 to 100 prime numbers using for loop?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you print non-prime numbers in java?

Loop through some numbers - for example, 2 through 100 - and check each one whether it is a prime number (write a second loop to test whether it is divisible by any number between 2 and the number minus 1). If, in this second loop, you find a factor that is greater than 1 and less than the number, it is not a prime, and you can print it out.


How do you print square using for loops?

how to print "square" using for loop


To print 5 45 345 2345 12345 in qbasic programming?

Oh, dude, to print those numbers in QBasic, you can use a simple loop. Just loop from 1 to 5 and print the numbers with spaces in between. It's like making a sandwich, but with numbers instead of bread and cheese. So, like, don't stress, just code it up and hit run. Easy peasy, right?


What is odd loop?

Move the print out requesting the user to enter an integer outside of the for loop and it will only print once instead of each time around the loop. You'll need a way to save the even and odd numbers that you detect in the loop. One way is to have separate arrays to hold the even and the odd numbers as you go around the loop. Then at the end of the loop you can have more loops to print the contents of one array and then the contents of the other array. Another way is to concatenate the number onto separate Strings (even and odd) to be displayed after the data gathering loop.


Write an algorithm or draw a flowchart to display numbers from 100 down to 10?

n=100 loop until n = 9 print n n = n -1 end loop

Related questions

How do you print non-prime numbers in java?

Loop through some numbers - for example, 2 through 100 - and check each one whether it is a prime number (write a second loop to test whether it is divisible by any number between 2 and the number minus 1). If, in this second loop, you find a factor that is greater than 1 and less than the number, it is not a prime, and you can print it out.


How do you write a program to print numbers to 50 except prime?

First, create a for loop from a,1 to 50. Inside of that create another for loop b,2 to a-1. If a/b=int(a/b) then you know it is not prime


How do you print square using for loops?

how to print "square" using for loop


How can print prime numbers in between 1-100 but not adjust 40-50 in php language?

Use a counted loop in the closed range [1:100]. If the count is in the closed range [40:50], print the number. For all other numbers outwith this range, only print the number if it is prime.


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

I suggest you write an outer loop, that loops through all the numbers from 1 to 50. Call the variable that controls the loop something like "number".Then, initially assume the number is a prime (with a variable such as "isprime"); and write another loop that compares whether the number is divisible by any number from 2 to number-1. Call the controlling variable "factor". If number is divisible by factor, then it is not a prime, and you can exit the loop. At the end of the inner loop, if the variable "isprime" is still true, then you print the number.


To print 5 45 345 2345 12345 in qbasic programming?

Oh, dude, to print those numbers in QBasic, you can use a simple loop. Just loop from 1 to 5 and print the numbers with spaces in between. It's like making a sandwich, but with numbers instead of bread and cheese. So, like, don't stress, just code it up and hit run. Easy peasy, right?


How do you print 1 to 100 without using loop in php?

The best way to print the numbers 1 to 100 in PHP without using a loop is with the following code: echo implode("&lt;br&gt;", range(1,100)); You can replace the &lt;br&gt; with anything that you want to separate the numbers, such as dashes. I used a line-break in the example.


How to draw Flowchart to print prime numbers from 1 to 100 using while loop in c language?

Oh, dude, drawing a flowchart for printing prime numbers from 1 to 100 using a while loop in C? That's like asking me to explain quantum physics while juggling flaming torches. But hey, you basically start with a start symbol, then draw a decision box to check if a number is prime, and loop back until you reach 100. Just remember to add some arrows and shapes, and you're good to go!


How do you print the numbers 1 through 50 using a do-loop while statement in Visual Basic.NET?

x as int = 0 while x &lt; 50 x = x + 1 print x endwhile


To print unique no using for loop?

int main (void) { puts ("unique"); }


What is odd loop?

Move the print out requesting the user to enter an integer outside of the for loop and it will only print once instead of each time around the loop. You'll need a way to save the even and odd numbers that you detect in the loop. One way is to have separate arrays to hold the even and the odd numbers as you go around the loop. Then at the end of the loop you can have more loops to print the contents of one array and then the contents of the other array. Another way is to concatenate the number onto separate Strings (even and odd) to be displayed after the data gathering loop.


Write a program using while loop?

//program to find the factorial value f any number using while loop #include&lt;stdio.h&gt; void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&amp;n); i=n; while (i&gt;=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user