0

How to make a program to determine if an integer is prime or not?

Hello everybody, I have to make a program to determine if a supplied integer is prime or not. At the same time it has to be within the range of 1 and 999, otherwise it will ask the user to enter a number within that range. It should also not be considered in case 1 is entered as a prime number. I have tried to do it but it always gives me an error that I do not understand where it comes from. Could you explain to me where I fail, The program I did so far: #include <stdio.h> int main () { int num, cont = 0, div = 1; printf ("enter a number"); scanf ("% d", &num); while (num> = 1 && num <= 999) { if (num% div == 0) { cont ++; } div ++; } if (cont == 2) { printf ("The number is prime"); } return 0; }

18th May 2021, 10:23 PM
Brian Ayvar
Brian Ayvar - avatar
1 Réponse
0
hey Brian Ayvar take loop from 2 (say counter) to less than number , see that counter completely divides that number or not , if it divides that mean that's not prime , exit ! >>>you can make efficient by taking loop from 2 to " half of number " !
19th May 2021, 1:36 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar