Write a program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and that outputs whether the number is prime.
If the number is not prime, then output all the numbers, from the list of the first 11 prime integers, which divide the number.
#include <iostream>
using namespace std;
int main() {
// Write your main here
int i, n;
bool isPrime = true;
double x;
cout << "Enter a positive integer between 1 and 1000: ";
cin >> n;
if (1 == isPrime) {
isPrime = true;
}
else {
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number.";
if (n != isPrime) {n == x;}
if (x/2 == isPrime) {cout << x/2 << " is divisibal by 2.";}
return 0;
}
I am trying to figure this out, but I am having trouble understanding the coding to divide the non-prime numbers by the 11 prime numbers listed above.
you can just hard code that part.
int p11[] {2,3,5,7,11,13,17,19,23,29,31};
for( everything in p11)
if num% p11[index] == 0 then it divides into it, print p[11] into your output list of factors.
you can also get mathy at it and multiply everything in p11 together into one integer and take the gcd (c++ has a gcd function, do not write it) in a while loop until you don't get any more factors (after finding a factor, divide both sides by it). Remember that factors can repeat: 8 is 2*2*2 when you do the prime factorization.
for (i = 2; i <= n / 2; ++i) {
^^ that can be only to sqrt n, not n/2. This is very significant for large n, but for large n, there are better ways to do it.
please use code tags. Is hard to read without. <> on the edit bar
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main() {
// Write your main here
int n;
int i[] = {2,3,5,7,11,13,17,19,23,29,31};
bool isPrime = true;
cout << "Enter a positive integer between 1 and 1000: ";
cin >> n;
if (1 == isPrime) {
isPrime = true;
}
else {
for (2,3,5,7,11,13,17,19,23,29,31) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number.";
return 0;
}
This is what I think you wanted me to do, but it is not working.
for(int dx = 0; dx < 11; dx++) //traditional for loop
if(n%i[dx] ==0) //use the array's values
{
isprime = false;
//or is the point to say which of the first 11 primes is a factor?
cout << i[dx] << ", ";
}
#include <iostream>
usingnamespace std;
bool is_prime(int n)
{
if( n == 1)
returnfalse;
else
{
for( int i = 2; i * i <= n; i++)
{
if(n % i == 0)
returnfalse;
}
}
returntrue;
}
int main()
{
int chosen_no = 0;
bool isPrime = false;
// Enter a positive integer between 1 and 1000 (inclusive)
while(chosen_no < 1 || chosen_no > 1000)
{
cout << "Enter a positive integer between 1 and 1000: ";
cin >> chosen_no;
}
// Output whether the number is prime
if( chosen_no == 1)
isPrime = false;
else
{
isPrime = true;
for( int i = 2; i*i < chosen_no; i++)
{
if(chosen_no % i == 0)
{
isPrime = false;
break;
}
}
}
cout << chosen_no;
if(isPrime == true)
cout << " is";
else
cout << " is not";
cout << " prime\n";
// Output first 11 primes and put in array
constint NO_OF_PRIMES{11};
int count{0};
int first_11_primes[NO_OF_PRIMES]{0};
for(int i = 1; i <= 1000; i++)
{
if(is_prime(i))
{
first_11_primes[count] = i;
count++;
}
if(count > NO_OF_PRIMES)
break;
}
for(int i = 0; i < NO_OF_PRIMES; i++)
cout << i + 1 << '\t' << first_11_primes[i] << '\n';
// If the number is not prime, then output all the numbers
// from the list of the first 11 prime integers, which divide the number
if( !is_prime(chosen_no) )
{
cout << chosen_no;
for(int i = 0; i < NO_OF_PRIMES; i++)
{
if(chosen_no % first_11_primes[i] == 0)
{
cout << " is divisible by " << first_11_primes[i] << '\n';
}
}
}
return 0;
}
Enter a positive integer between 1 and 1000: 899
899 is not prime
1 2
2 3
3 5
4 7
5 11
6 13
7 17
8 19
9 23
10 29
11 31
899 is divisible by 29
is divisible by 31
Program ended with exit code: 0
#include <iostream>
int main()
{
constint primes[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
int chose {};
while (chose < 1 || chose > 1000) {
std::cout << "Enter a positive integer between 1 and 1000: ";
std::cin >> chose;
}
bool isp {true};
for (constint p : primes)
if (p != chose)
if ((chose % p) == 0) {
if (isp)
std::cout << chose << " is not prime\n";
std::cout << "It is divisible by " << p << '\n';
isp = false;
}
if (isp)
std::cout << chose << " is prime\n";
}
Enter a positive integer between 1 and 1000: 105
105 is not prime.
It is divisible by 3
It is divisible by 5
It is divisible by 7
Enter a positive integer between 1 and 1000: 19
19 is prime