#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
int main()
{
    int i,j=0;
    cout<<"1";
for(i=1;i<=55;i++){
               if(j>12)
               break;
               else{      
if(i%2==0||i%3==0||i%5==0)

j++;

}

}

getch();
return 0;


}

This is my code .I want to print the numbers whose only prime factors are 2, 3 or 5. The sequence

1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ... with a limit such as first 11 prime.
But this code does not compile after the 12th loop.Please help.

Recommended Answers

All 11 Replies

Hm, not sure if i understand what are you talking about, are you saying that the program just ends after the 12th loop or something else, please be more specific.

j is incremented by one each time through the loop. On line 11 is the instruction to break out of the loop if j is over 12. So it seems like the code is doing what it has been written to do.

1 has not prime factor 2,3 or 5. If you had all numbers less than n satisfying the condition available, could you use those numbers to produce numbers which also satisfy the condition and are bigger than or equal to n? Don't you know which numbers less than 6 satisfy given condition?

You should also consider printing something besides the initial "1" if you want to see a list of numbers.

If a number's ONLY prime factors are 2, 3, and/or 5, then you should be able to divide evenly by some quantity of each of those factors, and be left with 1.

bool factorsAre2And3And5(int val)
{
    if (val < 1)
        return false;
    while (val > 1) {
        if (val % 2 == 0)
            val /= 2;
        else if (val % 3 == 0)
            val /= 3;
        else if (val % 5 == 0)
            val /= 5;
        else
            break;
    }
    return (val == 1);
}

nuclear
I want to print first 12 primes.Now it will be clear to you.But I can not find the problem.Please try to solve it.

raptr dflo
I do not understand what you want to say.Please expain.

Anuradha,
You said in your original post:
>> This is my code .I want to print the numbers whose only prime factors are 2, 3 or 5.

If you want to print the first 12 primes, that's a different exercise!

1 is not a prime number (by consensus, as it's also not a product of prime factors, it's just a special case). Each number greater than 1 is either a prime number or is the product of some set of prime factors, each of which is smaller than the number:
2 is prime, 3 is not divisible by 2 so 3 is prime, 4 is the product of 2 and 2, and so on. Each time you discover a prime, print it out and increment your counter, until you have printed however many you want (in your case 11).

int count = 0;
int potential = 2;
while (count < 11) {
    bool is_prime = is_num_prime(potential);  // determine if potential is prime
    if (is_prime) {
        cout << potential << " is prime" << endl;
        count++;
    }
    potential++;
}

I've left the is_num_prime() function for you to complete (if any number smaller than the number you're testing divides it evenly, then the number is not prime -- there are a number of efficiencies you can make -- think about: which numbers should you divide by? when can you stop?). Get it working first, then do a search on "sieve of eratosthenes" for an approach to finding primes < N by eliminating non-primes from a list.

Enjoy!

Well if you only want to show the first 12 numbers then this should work:

#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
int main()
{
    int i,j=0;
    cout<<"1 ";
for(i=1;i<=55 && j < 11;i++)
{ 
if(i%2==0||i%3==0||i%5==0)
{
j++;

cout << i << " ";
}
}

getch();
return 0;
}
Member Avatar for HASHMI007
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
//using namespace std;
int main()
{
	 int i,j=0;
	 cout<<"1";
for(i=1;i<=55;i++){

if(i%2==0||i%3==0||i%5==0)
cout<<" "<<i;
j++;

}


getch();
return 0;


}
commented: Why no description? +0
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
//using namespace std;
int main()
{
	 int i,j=0;
	 cout<<"1";
for(i=1;i<=55;i++){

if(i%2==0||i%3==0||i%5==0)
cout<<" "<<i;
j++;

}


getch();
return 0;


}

What was this code posted for? Without any description at all, my guess is how not to program in C++ since about 1/2 the code is bad and the formatting is pathetic making it hard to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.