#include <iostream>
using namespace std;
long int math;
long int check;
long int divider = 1;
long int inputnum;
void dividerp();
void counter();
void inputp();

void dividerp(){
     math = inputnum/divider;
     if (math == 1 or inputnum == 2 or inputnum == 1){
              cout<<"You do have a prime number there!"<<endl;
              system("PAUSE");
              system("CLS");
              math = 0;
              inputp();
              }
     else if (math == inputnum){
          counter();
          }
     else if ( inputnum % divider != 0 ){
          counter();
          }

     else if (math >= 1){
              cout<<"You do not have a prime number!"<<endl;
              system("PAUSE");
              system("CLS");
              math = 0;
              inputp();
              }
     else{
              counter();
              }
     }
     
void counter(){
     divider = divider + 1;
     dividerp();
     }
     
void inputp(){
     divider = 1;
     cout<< "Please type in a number."<<endl;
     cout<< "I will check to see if it is a prime number."<<endl;
     cin>> inputnum;
     if (inputnum >= 1){
               dividerp();
               }
     else {
          cout<<"Sorry you have not typed in an accurate number."<<endl;
          system("PAUSE");
          system("CLS");
          inputnum = 0;
          inputp();
     }
     }
     
int main(){
    cout<< "Hello and welcome to the program."<<endl;
    inputp();
}

I'm still learning, (Went back from allegrogl, totally forgot how to do this stuff) Is there a way to improve this code, or types of functions that can improve performance (Not like there is a problem with it). And how can I increase the number range that I can use to find more prime numbers? I can't use floats/doubles due to the fact that they for some reason do not like to have math applied to them, is there a way to fix that?

EDIT: Optimizing code, that's the word i'm looking for. How can I optimize code?

Recommended Answers

you could use a simple for loop in your main function and do a brute force method like your functions do or you could use the Sieve of Eratosthenes method which works very well.

Jump to Post

When testing for primes, you only need to test half the range:

bool is_prime = true;

cout << "Enter a numeral: ";
cin >> number;

if(number == 1 || number == 3)
{
     cout << "Number is prime.";
     exit(0);
}

for(int i=2; i<number/2; i++)
{
     //If there …
Jump to Post

All 6 Replies

you could use a simple for loop in your main function and do a brute force method like your functions do or you could use the Sieve of Eratosthenes method which works very well. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

also on line 13 in your code you are using or which is incorrect in c++. || = or and && = and in c++

When testing for primes, you only need to test half the range:

bool is_prime = true;

cout << "Enter a numeral: ";
cin >> number;

if(number == 1 || number == 3)
{
     cout << "Number is prime.";
     exit(0);
}

for(int i=2; i<number/2; i++)
{
     //If there is any case of no remainder, then number is not prime
     if(number%i)
     {
          is_prime = false;
          break;
     }
}

if(is_prime)

     cout << number << " is a prime number. ";

else 

     cout << number << " is not a prime number. ";

There might be easier ways to do this.. this is just something I came up with off the top of me' head.

also on line 13 in your code you are using or which is incorrect in c++. || = or and && = and in c++

I'm using Dv-C++ 4.9.9.2 writer and compiler and it said that "or" was ok when it was compiling, was just having a brain fart :P

i bow before your compiler then ;). seriously i would use the standard operators since i doubt it would work on older compilers.

Other than one post which could clean up my code, I have gotten no useful replies. I just want to improve my skill and learn new functions that might be helpful, for I have not any other means of doing so.

Be a part of the DaniWeb community

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