Home > OS >  Building prime number checker in c
Building prime number checker in c

Time:01-05

I am new to c and coding so excuse me if my knowledge is wrong. I was in bed and thought of a way to check if a number is prime (my code is below). My theory is that the user inputs a number, and using a for loop, variable i increases in 1 increments until it equals the inputted number. each time this increment happens, it is tested for modulus, therefore if its a factor the result will be zero. if it equals zero, a 1 increment happens to a variable(remainders), and after all the values for i are tested, if the value of remainders is 2 it is prime, if not it is not prime. My code is below, please inform me whether it is my idea, and/or my execution.



#include <iostream>
using namespace std;

int main () {
int userNumber;
int remainders = 0;
cout << "Input the number to be tested:";
cin >> userNumber;

for (int i = 0; i == userNumber; i  ){
        int remainderTest = userNumber % i;
    if (remainderTest = 0) {
        remainders   ;
    }
}

if (remainders == 2) {
    cout << "The number you inputted is prime!";
} else {
cout << "The number you entered is not prime!";
}

return 0;
}


CodePudding user response:

Your code has an in the for loop. Instead of i == userNumber, it should be i < userNumber. Here's an implementation of your idea as a function:

bool isPrime(int n) {
    bool isPrime = true;
    for (int i = 2; i<n; i  ) {
        if (n%i == 0) isPrime = false;
    }
    return isPrime; 
}

You don't need to account for 1 and n being factors of any n if you don't include them in the for loop condition by starting at 2 and ending at n, so there is no need to keep track of remainders through a variable.

CodePudding user response:

Firstly, i think that you should declare i=2, because it is useless to modulo 0 or 1. Secondly, why you declare i=0, and after that i==userNumber :)) Beside, in line 12, it ==, not =. Your code (after fixed):

#include <iostream>
using namespace std;

int main () {
int userNumber;
int remainders = 0;
cout << "Input the number to be tested:";
cin >> userNumber;

for (int i = 2; i <= userNumber; i  ){
        int remainderTest = userNumber % i;
    if (remainderTest == 0) {
        remainders   ;
    }
}

if (remainders == 1) {
    cout << "The number you inputted is prime!";
} else {
cout << "The number you entered is not prime!";
}

return 0;
}

CodePudding user response:

OP's code fails in various ways.

  • Code atmepts userNumber % 0 which is undefined behavior (UB).

  • Code assigns rather than compares with if (remainderTest = 0)

OP's code is slow: O(n)


A short prime test algorithm to help OP along.

It iterates until i exceeds n/i.

bool prime_test(int n) {
  for (int i = 2; i <= n/i; i  ) {
    if (n%i == 0) return false;
  }
  return n > 1; 
}

Of course there are many potential operations with more code.

The one above is very short, yet still fast: O(sqrt(n)) and works for all int n.

  •  Tags:  
  • Related