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.
#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;
}
Comments
Leave a comment