Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that finds and prints all of the prime numbers between 3 and 100

ID: 441750 • 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 (3,5,7,11,13,17,...). 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 ways to make this algorithm efficient). Must include comments in your code. I am using the book "Absolute C++" by Walter Savitch 5th Edition and this is number 4 on page 94.

Explanation / Answer

/** cpp code to calculate primes b/w 3 and 100**/


#include<iostream>

using namespace std;

int main(){

int i,j,flag;

cout<<"the prime numbers between 3 and 100 are ";

for(i=3;i<100;i++){

flag=0;

for(j=2;j<i;j++){ // j=2 to j<=(i/2) or j=2 to j<=sqrt(i) will make code more efficient

if(i%j==0){flag=1;break;}

}

if(flag==0)cout << i<<" ";

}

cout<<" ";

return 0;

}


// note: to make program more efficient , in the inner loop u can vary j from 2 to (i/2) or sqrt(i)

//this will make program more efficient

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote