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

A prime number is defined as any number that is only divisible by itself and 1.

ID: 3566358 • Letter: A

Question

A prime number is defined as any number that is only divisible by itself and 1. For instance the
numbers 2, 17, and 53 are all prime numbers. A composite number is simply defined as any number
that is not a prime number. For your program you will create a function called isComposite() which
takes an integer as an argument and returns a boolean value (true if the integer is composite and false if
otherwise.) As an example the following function call will return true:


isComposite(16)


Your program will then find the first 100 composite numbers and output the result to a file called
composite.txt (with the main() function calling isComposite() in the process.)

write in C++

Explanation / Answer

#include<iostream>
#include<math.h>
#include<fstream>

using namespace std;

bool isComposite(int num){
for(int i= 2; i<= sqrt(num); i++){
if(num%i == 0){
return true;
}
}
return false;
}

int main(){
ofstream outfile;
outfile.open("composite.txt");
int i = 3;
int count = 0;
while(1){
if(isComposite(i)){
outfile << i << endl;
count++;
}
if(count == 100){
break;
}
i++;
}
return 0;
}

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