Write a program that finds and prints all of the prime numbers between 3 and 100
ID: 3693638 • Letter: W
Question
Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number that can only be divided by one and itself (i.e., 3, 5, 7, 11, …). 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 whether the counter value for the outer loop is prime. One way to decide whether the number n is prime is to loop from 2 to n-1; if any of these numbers evenly divides n, then n cannot be prime. If none of the values from 2 to n-1 evenly divide n, then n must be prime. (Note that there are several easy ways to make this algorithm more efficient.) Hint: use modulus division and a flag (boolean) variable for efficiency In C++
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int i, j;
bool flag;
cout<<"Primes from 3 to 100 are: ";
for(i=3;i<=100;i++)
{
flag = true;
for(j=2;j<i;j++){
if(i%j==0){
flag = false;
break;
}
}
if(flag)
cout<<i<<" ";
}
cout<<endl;
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.