problem for "for loop"

Sep 30, 2009 at 10:43am
#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 ();

}

i have a hard time looking for the error with this program.. it doesn't run.. whats the correction with this?
Sep 30, 2009 at 12:02pm
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.
Sep 30, 2009 at 1:10pm
Why are you using goto to achieve something so simple. I would suggest you to rethink of the solution to this problem and try a totally new logic.

-Kush
Oct 1, 2009 at 3:17am
i have no more idea onhow to improve it. can u help me formulate another code for this?
Oct 1, 2009 at 5:12am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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.
Last edited on Oct 1, 2009 at 5:21am
Oct 1, 2009 at 5:54am
Ok in that case can you give us a clear cut problem definition,...

problem definition means what is that you want to achieve from the program....... Define your problem in very simple words..

-Kush
Oct 1, 2009 at 1:09pm
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.

Someone an idea?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector> 

using namespace std;

void main(){
	
	vector <unsigned long int> primelist;  
	for (long unsigned 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 (long unsigned int j=2;j<i;j++){
			if (i%j == 0){
				prime=false;
				break;
			}
		}
		if (prime)
			primelist.push_back(i); //insert all prime numbers between 1000 and 9999
	}

	for (unsigned int a=0;a<primelist.size()-4;a++){
		bool prime = true;
		bool breakloop = false;
		for (unsigned int b=a+1;b<primelist.size()-3;b++){
			for (unsigned int c=b+1;c<primelist.size()-2;c++){
				for (unsigned int d=c+1;d<primelist.size()-1;d++){
					for (unsigned int e=d+1;e<primelist.size();e++){
						unsigned int produkt = primelist[a] * primelist[b] * primelist[c] * primelist[d] * primelist[e];
						if (produkt%2 == 0 || produkt%5 == 0) 
							continue;
						prime=true;
						for (unsigned int teiler=2;teiler<produkt;teiler++){
							if (produkt%teiler == 0){
								prime=false;
								break;
							}
						}
						if (prime){
							cout << "Found following prime numbers " << " " << primelist[a] << " " << primelist[b] << " " <<
							primelist[c] << " " << primelist[d]<< " " << primelist[e] << endl;
							cout << "Product of these numbers, also a prime number, is " << produkt << endl;
							breakloop=true;
							break;
						}
					}
					if (breakloop) break;
				}
				if (breakloop) break;
			}
			if (breakloop) break;
		}
		if (breakloop) break;
	}
}
Last edited on Oct 1, 2009 at 1:10pm
Oct 2, 2009 at 1:22pm
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?


Oct 2, 2009 at 1:36pm
actually the problem of this source code that to be solve is....

find five four prime digit no. duch that the product of each four digit prime no. is also prime.
Oct 2, 2009 at 2:52pm
Here is the solution. You have to define your problem as accurate as you can, otherwise the people dont know whats your problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector> 

using namespace 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] << " ";
	}
}
Last edited on Oct 2, 2009 at 3:06pm
Oct 3, 2009 at 1:35am
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..

7 `main' must return `int'
Last edited on Oct 3, 2009 at 1:43am
Oct 3, 2009 at 1:55am
got it... i know the answer to my problem fenor.. thank you so much for your help.. highly appreciated.
Last edited on Oct 3, 2009 at 2:12am
Topic archived. No new replies allowed.