Write a program that prompts the user to enter an integer and then displays that integer as a product of its primes and if it is a prime then it should say so??

Above is the question I have been given

#include <iostream>

#include <vector>

using namespace std;

int main ()

{

int num, result;

cout << "Enter A Number " << endl;

system ("pause");

return 0;

}

This is what I have so far, do I have to use a for loop, a while loop or a do loop,

and if it is possible can someone give me an example of how to it.

thanks very much appreciated

Above is the question I have been given

#include <iostream>

#include <vector>

using namespace std;

int main ()

{

int num, result;

cout << "Enter A Number " << endl;

system ("pause");

return 0;

}

This is what I have so far, do I have to use a for loop, a while loop or a do loop,

and if it is possible can someone give me an example of how to it.

thanks very much appreciated

Use this to check for for prime numbers.

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.

Basically your program is asking to find factors, prime factors.

Basically, this exercise is preparing you for a particular cryptography concept, such as RSA

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.

Basically your program is asking to find factors, prime factors.

Basically, this exercise is preparing you for a particular cryptography concept, such as RSA

Last edited on

Guys,

I have changed the code around to this,

#include <iostream>

using namespace std;

int main() {

// Declaring Variables

int perfect;

int counter;

int num1;

//initialize variables

cout<<"This program will tell you the prime factorization of a number"<<endl;

cout<<"Please input a number and press [ENTER]."<<endl;

cin>>num1;

for (counter=1;counter<=num1;counter++)

{

if (num1%counter==0)

cout<<"The possible factors of that number are "<< counter <<endl;

}

return 0;

}

and it is showing me all the possible factors of the number that I enter, I only want it to show the Prime numbers

I have changed the code around to this,

#include <iostream>

using namespace std;

int main() {

// Declaring Variables

int perfect;

int counter;

int num1;

//initialize variables

cout<<"This program will tell you the prime factorization of a number"<<endl;

cout<<"Please input a number and press [ENTER]."<<endl;

cin>>num1;

for (counter=1;counter<=num1;counter++)

{

if (num1%counter==0)

cout<<"The possible factors of that number are "<< counter <<endl;

}

return 0;

}

and it is showing me all the possible factors of the number that I enter, I only want it to show the Prime numbers

I have now changed my code to this,

#include <iostream>

using namespace std;

int main()

{

int n, c = 2;

printf("Enter a number to check if it is prime\n");

scanf("%d",&n);

for ( c = 2 ; c <= n - 1 ; c++ )

{

if ( n%c == 0 )

{

printf("%d is not prime.\n", n);

break;

}

}

if ( c == n )

printf("%d is prime.\n", n);

system ("pause");

return 0;

}

can anyone explain to me on how i can get the prime numbers shown of the number that is entered

#include <iostream>

using namespace std;

int main()

{

int n, c = 2;

printf("Enter a number to check if it is prime\n");

scanf("%d",&n);

for ( c = 2 ; c <= n - 1 ; c++ )

{

if ( n%c == 0 )

{

printf("%d is not prime.\n", n);

break;

}

}

if ( c == n )

printf("%d is prime.\n", n);

system ("pause");

return 0;

}

can anyone explain to me on how i can get the prime numbers shown of the number that is entered

Last edited on

Topic archived. No new replies allowed.