# Jumping into C++ problems

• 01-14-2013
Jumping into C++ problems
Hi, I have written the code of the question "Design a program that finds all numbers from 1 to 1000 whose prime factors, when addedtogether, sum up to a prime number (for example, 12 has prime factors of 2, 2, and 3, which
sum to 7, which is prime). Implement the code for that algorithm."
the code is
Code:

``` #include <iostream> using namespace std; bool isdivisible(int z,int i) {     return z%i==0; } bool (isprime (int z)) {   for (int h=2;h<z;h++)   {   if (isdivisible(z,h))     {       return false;     }   }     return true; } int main() {     for ( int k = 2;k<10; k++) {     int i=2;     int y=0;     int x;     x=k;         while(i<=x)     {         if (x%i==0)         {           x=x/i;           y=y+i;         }         else         {i++;}     }     int z;     z=x+y-1;     if (isprime(z))     {         cout<<k<<endl;     }     } }```
the problem is that the console window cant display all the results. It omits the 1st few result. How can I increse the space so that it can display all the results?
• 01-14-2013
Shakti
I gave you a link that describes how to write to a file in this thread: http://cboard.cprogramming.com/cplus...problem-2.html

• 01-14-2013
Quote:

Originally Posted by Shakti
I gave you a link that describes how to write to a file in this thread: http://cboard.cprogramming.com/cplus...problem-2.html

I have done what it says like this, nothing happens,

Code:

```#include <fstream> #include <iostream> using namespace std; bool isdivisible(int z,int i) {     return z%i==0; } bool (isprime (int z)) {   for (int h=2;h<z;h++)   {   if (isdivisible(z,h))     {       return false;     }   }     return true; } int main() {     for ( int k = 2;k<10; k++) {     int i=2;     int y=0;     int x;     x=k;         while(i<=x)     {         if (x%i==0)         {           x=x/i;           y=y+i;         }         else         {i++;}     }     int z;     z=x+y-1;     if (isprime(z))     {  char str[10];         ofstream file ("example");         file<<z;         file.close();     }     } }```
• 01-15-2013
King Mir
Quote:

Originally Posted by Tamim Ad Dari
the problem is that the console window cant display all the results. It omits the 1st few result. How can I increse the space so that it can display all the results?

You can pipe the output to a pager, like "more" or "less" as in:
\$program | more
• 01-16-2013
Aslaville
I think the problem lies in your algorithm,,,the console window is capable of displaying thousands of values,..let me check it out:tongue:
• 01-16-2013
Matticus
Quote:

Originally Posted by Tamim Ad Dari
I have done what it says like this, nothing happens,

Did you read the link in post #2 thoroughly?

You should open/create the file before the main loop, and close the file after the main loop. As of now, the file is opened/closed each time "if(isprime(z))" is true. And, according to the link, "The default mode for opening a file with ofstream's constructor is to create it if it does not exist, or delete everything in it if something does exist in it."

Also, create the file name with an extension that's easy to view (i.e. "example.txt").
• 01-16-2013
Aslaville
The algorithm is perfect.
Maybe you can avoid writing every number to on a new line,,...instead you could just add a space between numbers.