/*
Program that create an array named numbers of size 60 and initialize it randomly from 10 to70 range now take
2 more arrays, 1 for prime numbers and 1 for composite numbers. Traverse the array numbers and check whether
it is a prime number if it is then store this number to prime array else in composite array.
*/
#include<iostream>
#include<ctime>
#include<cstdlib> //for srand(time(0))
using namespace std;
bool isPrime(int); //function Declaration/Prototype
int main()
{
int numbers[60], prime[60], composite[60], random;
//For Inputting amount of numbers
srand(time(0));
//for initializing numbers[] with random numbers within (10-70)
for (int i = 0; i < 60; i++)
{
do //generates random number within (10-70)
{
random = rand();
} while (random <= 10 || random >= 70);
numbers[i] = random;
}
//for displaying numbers[]
cout << "\nRandom Numbers in array are:\n\t";
for (int i = 0; i < 60; i++)
{
cout << numbers[i];
if (i < 59)
cout << ",";
}
cout << endl;
//for formatting output
for (int i = 0; i < 80; i++)
cout << "=";
int p = 0, c = 0;
for (int i = 0; i < 60; i++)
{
if (isPrime(numbers[i]))
prime[p++] = numbers[i];
else
composite[c++] = numbers[i];
}
//For displaying Prime numberss
cout << "\nTotal " << p << " Numbers are Prime Numbers in array which are:\n\t";
for (int i = 0; i < p;i++)
{
cout << prime[i];
if (i < p-1)
cout << ",";
}
//For displaying Composite numberss
cout << "\n\nTotal " << c << " Numbers are Composite Numbers in array which are:\n\t";
for (int i = 0; i < c; i++)
{
cout << composite[i];
if (i < c - 1)
cout << ",";
}
cout << endl << endl;
return 0;
}
//function Definition of isPrime()
bool isPrime(int num)
{
//for checking entered number that whether it is prime or composite
for (int i = 2; i <= num/2; i++)
if (num % i == 0)
return 0;
return 1;
}
OUTPUT
No comments:
Post a Comment