Answer to Question #230523 in C++ for ahmad

Question #230523

Write a C++ program to declare an integer array of 50 elements. Fill this array using random function with values in range -25 to +75. Now, print address(s) of only those indexes which are having a value which is a prime No.


 


1
Expert's answer
2021-08-29T00:58:16-0400
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
//Implement function check number to prime 
bool isPrime(int n)
{
	for (int i = 2; i * i <= n; i++)
		if (n % i==0)
			return false;
	return true;
}
int main()
{
	srand(time(0));
	int ar[50];


	//[-25;+75 ] random generate [a;b]=random%(b-a)+a
	for (int i = 0; i < 50; i++)
	{
		ar[i] = rand() % (75 + 25) - 25;
	}
	cout << "==============Numbers=============================\n";
	for (int i = 0; i < 50; i++)
	{
		cout << ar[i] << "  ";
	}
	cout << endl<<"====================Answer==========================\n";
	//Out index no Primes
	for (int i = 0; i < 50; i++)
	{
		if (!isPrime(ar[i]))
		{
			cout << "Index=" << i + 1 << "     Value=" << ar[i] << "   Adress=" << &ar[i] << endl;
		}
	}
	return 0;
}

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