Program wont print prime numbers


 
Thread Tools Search this Thread
Top Forums Programming Program wont print prime numbers
# 1  
Old 10-28-2012
Program wont print prime numbers

The problem I'm having is that when you put in the two numbers the answer is just prime.... nothing. I cannot figure this out ive been working on this forever, can someone please god just tell me how to fix this without encrypted "hints".



Code:
#include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPE FOR read_range
void read_range(int& imin, int& imax);

// FUNCTION PROTOTYPE FOR is_prime
bool is_prime(int& k);

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
  int imin(0), imax(0);
  

  // Read in range
read_range(imin, imax);

// Print prime numbers

 cout << "Primes:";
 for (int k = imin; k <= imax; k++)
   {
     if (is_prime(k)) 
       { 
     cout << "  " << k; 
       }
   }
 cout << endl;
 
 return 0;
}

// DEFINE FUNCTION read_range() HERE:
void read_range(int& imax, int& imin)
{
  cout<<"Enter minimum and maximum:";
  cin>>imin>>imax;
  while(imin>imax)
    {
     cout<<"Error, Minimum must be less then maximum"<<endl;
     cout<<"Enter minimum and maximum:";
     cin>> imin>>imax;
    }
  while((imin<2 || imax<2))
    {
      cout<<"Error, Minimum and maximum must be at least 2"<<endl;
      cout<<"Enter minimum and maximum:";
      cin>> imin>>imax;
    }
}


// DEFINE FUNCTION is_prime() HERE:
bool is_prime(int& k)
{
  
  for(int i=2; i<=(k-1); i++)
    {
      if (k%i !=0)
    true;
      
    }
    
  
}

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!
# 2  
Old 10-28-2012
Why do I get the feeling this is a homework assignment that is not posted to the homework forum?
# 3  
Old 10-28-2012
Moderator's Comments:
Mod Comment Note to c++newb - Insulting Forum Members Who Ask You Questions or Posting Homework in the Wrong Forums will Result in Being Banned.
# 4  
Old 10-28-2012
Hi c++newb,
your first 2 posts were in the Homework section and this "problem" too seems like a homework problem. So, it's a request to post the problem/question in the proper forum to get a response on proper lines.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

2. UNIX for Dummies Questions & Answers

Print last row of numbers

I have a spreadsheet of extremely long rows of numbers. I want to print only the last column. Tried using printf but there seems to be too many rows. example: 3 100 34 7 23 0 8 ..... X 400 203 778 1 ..........Y 58 3 9 0 100 ..........Z I only want to print X, Y and... (1 Reply)
Discussion started by: jimmyf
1 Replies

3. Homework & Coursework Questions

program to find and print a Fibonacci sequence of numbers. --Errors

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to convert a C language program over to Sparc Assembley and I am getting Undefined first referenced... (4 Replies)
Discussion started by: kenjiro310
4 Replies

4. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

5. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

6. Shell Programming and Scripting

The script wont trigger it. Thief Catcher program (open source)

me and my team working on a project called Stealth Hunter, Summarize about our project: Stealth Hunter is a thief catcher, It will silently take a snapshot of user using a stolen notebook or pc with webcam and send the information via email. how it work is: The scripts will triggered by the... (0 Replies)
Discussion started by: Kiraichi
0 Replies

7. Solaris

Purpose of RPC program numbers?

Whats the purpose of the following RPC program numbers: 100249 300598 805306368 Why the above program numbers were not listed in /etc/rpc file? Is it dynamically assigned? These program numbers doesnt persist after rebooting the system. If any of the service is using this program number then... (0 Replies)
Discussion started by: ramnagaraj
0 Replies

8. Shell Programming and Scripting

Prime Number Program (Fun)

Hi, I was just wondering if anyone has, or knows where to download a prime number finder program. I would like a fairly simple bash program, and also I would like one that could take advantage of multiple processors. I have 500 cores I can use, and would like to take advantage of them using a... (2 Replies)
Discussion started by: Kweekwom
2 Replies

9. Post Here to Contact Site Administrators and Moderators

program to find the uncommon numbers between two files.

Hi, I need to extract the uncommon numbers from file1 and file2 For Example: File1 1 2 3 4 5 File2 1 2 3 4 5 6 (2 Replies)
Discussion started by: salaathi
2 Replies
Login or Register to Ask a Question