printf("Five four prime digit numbers with there products:\n");
while(n!=5) //to get the five prime numbers with their products also a prime
{
x=0;
for ( i=1000 ; i<10000; i++) //to look for the prime numbers from 1000 to 9999
{
while (x!=1)
{
for (j=3; j<=i ; j++)
{
if ((i%j)==0) x=0; //modulo.to get the remainder
else
{
x=1;
goto B;
}
}
B:
{
}
}
n++;
}
getch ();
}
i have a hard time looking for the error with this program.. it doesn't run.. whats the correction with this?
It's very difficult to read through unformatted code. Put your code inside code tags with the proper indentation, otherwise we'll all have a hard time looking for the error.
#include <stdio.h>
#include <math.h>
main()
{
int i[10],j,c,sum,b,x;
printf("Five four prime digit numbers with there products:\n");
while(n!=5) //to get the five prime numbers with their products also a prime
{
x=0;
for ( i=1000 ; i<10000; i++) //to look for the prime numbers from 1000 to 9999
{
while (x!=1)
{
for (j=3; j<=i ; j++)
{
if ((i%j)==0) x=0; //modulo.to get the remainder
else
{
x=1;
goto B;
}
}
B:
{
}
}
n++;
}
getch ();
Where is the output instruction? and what does goto do? The i in the for loop is not declared
You should start with a successful method of determining a prime number up to a certain bound.
Here is my suggestion, but it needs to be fixed. First of all, the programm insert all prime numbers between 1.000 and 10.000, then it multiply 5 prime numbers on each loopwalkthrough until the product of these 5 Numbers is identified as a prime number. The output number is indeed a prime number, but the result isnĀ“t the product of these 5 numbers.
I think the problem is the * operator, it doesnt work with such high numbers.
fenor thank you so much though theres some problem with the source code,, i can handle it.. hmm... i have question, whatz the use of using namespace std; in the program?
#include <iostream>
#include <vector>
usingnamespace std;
void main(){
vector <int> primelist;
for (int i=1000; i<10000; i++){
if (i%2 == 0 && i != 2 || i%5 == 0 && i != 5) // all straight numbers and numbers with 0 or 5 ending cant be prime numbers
continue;
bool prime=true;
for (int j=2;j<i;j++){
if (i%j == 0){
prime=false;
break;
}
}
int tmp = i,product=1;
int primearray[4];
for (int k=0; k<4;k++){ // get the 4 digits of the number
primearray[k] = tmp%10;
tmp = tmp/10;
}
for (int k=0;k<4;k++){
product *= primearray[k]; //multiply the 4 numbers
}
if (product == 0)
prime = false;
else{
for (int l=2;l<product;l++){ //repeat prime check for the product of each number
if (product%l == 0){
prime=false;
break;
}
}
}
if (prime && primelist.size()<6)
primelist.push_back(i);
}
cout << "Found following prime numbers " << endl;
for (int m=0;m<5;m++){
cout << primelist[m] << " ";
}
}
thank you so much fenor and also sorry for making my problem to messy at first. y it doesn't appear? it runs but suddenly disappear the black box. if we put void beside the main(), there's some error ...this is the error..