Answer to Question #156079 in C++ for zain ul abdeen

Question #156079

Write a program that assigns Twenty random numbers to an array and counts all prime numbers entered by the user. The program finally displays a total number of primes in the array.


1
Expert's answer
2021-01-16T11:50:28-0500
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;


bool IsPrime(int x){
    if(x == 2) return true;//2 is prime
	if(x%2==0) return false;//check even


	for(int i=3;i*i<=x;i+=2) //check odd
		if(x%i==0) return false;


	return true;
}
int main(){
    srand(time(NULL)); //seed for random
    int array[20];
    int primeCounter = 0;
    for(int i = 0; i < 20; i++)
    {
        array[i] = rand()%10;//rand()%10 means random value from 0 to 10, 
                              //you can change this value or delete it
        if(IsPrime(array[i]))
            primeCounter++;
    }
    cout << primeCounter;


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS