Question: write a program that makes use of the isPrime function you've already written that scans a file (Big List of Random Integers.txt) of integers and
write a program that makes use of the isPrime function you've already written that scans a file (Big List of Random Integers.txt) of integers and determines the number of primes found within and display how many integers is prime.
#include
#include
#include
#include
#include
#include
#include
//method to check isPrime number is prime or not
int isPrime(long long int num) {
//variable declare
int i, isPrime = 1;
//for loop to check for prime to iterate and divide by number
for (i = 2; i < sqrt(num); i++) {
if (num % i == 0) {
isPrime = 0;
}
}
return isPrime;
}
int main() {
//file name to read integer from
FILE* file = fopen("Big List of Random Integers.txt", "r");
//storing in numberArray value
int numberArray[100];
int i = 0, j;
int num;
//reading from file and storing in numberArray
while (fscanf(file, "%d", &num) > 0) {
numberArray[i] = num;
i++;
}
int size = i;
//print the total number in the file
printf("There are %d number in the file ", size);
int max = 0;
//calculate the max number form the arry
for (i = 0; i < size; i++) {
if (numberArray[i] > max)
max = numberArray[i];
}
//print the maximum number
printf("The largest in the file is %d ", max);
int primeCount = 0, primeGenerated[50];
//calculate the consequetive number multiplcation and plus 2 and check it it prime or not
long long int primeNo;
//for loop to iterate
for (i = 0; i < size - 1; i++) {
//calculate the number
primeNo = numberArray[i] * numberArray[i + 1] + 2;
//check if it is prime or not by passing to the method we defined
if (isPrime(primeNo) == 1) {
//if the number is prime then print it
printf("%d * %d + 2 = %llu ", numberArray[i], numberArray[i + 1], primeNo);
primeGenerated[primeCount] = primeNo;
primeCount++;
}
}
//print the total number of primes found
printf("The total number of primes found: %d ", primeCount);
//calculate the greater than max value
int greaterThanMax = 0;
for (i = 0; i < primeCount; i++) {
if (primeGenerated[i] > max)
greaterThanMax++;
}
//print the total number of primes found, greater than
printf("The total number of primes found, greater than %d: %d ", max, greaterThanMax);
fclose(file);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
