-3

I've made an algorithm that in theory, should display every number between a range that can be manually inputted by a human. Its brain is programmed like so.

//Prime Number Displayer
#include <iostream>
using namespace std;

int main(){
int Low, High, check;


cout<<"Enter positive integer."<<endl;
cin>>Low;

cout<<"Enter a second positive integer."<<endl;
cin>>High;

cout<<"You will get a set of all prime numbers between "<<Low<<" and "<<High<<"."<<endl;

check=(Low%2);

    while (Low <=High){

if(check==1){

cout<<""<<Low<<""<<endl;

check++;
}
}




    return 0;
}

I want the program to display each prime number inside the range on an individual line. I can't seem to get any results other than the number on the low range being outputted and nothing else. Please and thank you.

thatguy
  • 7
  • 2
  • 4
    1) You are only checking if the `Low` divides by `2`, and **not** checking if any number, in range, is prime. 2) If `Low` does not divide by `2`, you output it, and keep increasing `check`, which means that it will never equal `1` again, in addition to looping infinitely. 3) So you are asking to write your entire program for you? Since "fixing" it, would require to do so. 4) Consider learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. – Algirdas Preidžius Jul 26 '18 at 08:36
  • You should learn how to format code properly – Jabberwocky Jul 26 '18 at 08:45
  • I think this while loop will go forever as low will always remain lower then High, as value of low is not changed/incremented in while loop. – Rizwan Jul 26 '18 at 08:47
  • This seems like another homework/tutorial question. I recommend following better tutorials before asking basic questions like this on S.O. – sɐunıɔןɐqɐp Jul 26 '18 at 09:01

1 Answers1

-3

A code like this will find prime number between the range given by user :

bool isPrime(int n)
{
    if (n <= 1)
        return false;

    if (n%2 == 0 && n != 2)
        return false;

    for (int i = 3; i < n; i+=2)
        if (n % i == 0)
            return false;

    return true;
}


int main()
{
    int Low, High;
    bool isPrimeNum;

    cout<<"Enter positive integer."<<endl;
    cin>>Low;

    cout<<"Enter a second positive integer."<<endl;
    cin>>High;

    cout<<"You will get a set of all prime numbers between "<<Low<<" and "<<High<<"."<<endl;


    while (Low <= High)
    {
        isPrimeNum = isPrime(Low);
        if (isPrimeNum)
            cout<<endl<<Low<<" is prime number.";
        Low++;
    }
}
Rizwan
  • 3,324
  • 3
  • 17
  • 38
  • Did you test your code, before posting it here? The `loop` variable will contain such values as `2`, `4`, `6`, `8`, `10`, ... Hence, your code will state that `9` is a prime. – Algirdas Preidžius Jul 26 '18 at 10:15
  • Yes I noticed it. I will correct this in some time. Not on system right now – Rizwan Jul 26 '18 at 10:17