File: Stare vježbe/vjezbe05/30__prosti_brojevi.c

  1. /*
  2.   30__prosti_brojevi.c
  3.   -----
  4.   Program provjerava da li je ucitani prirodan broj prost.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. #define JE_PROST 0
  11. #define NIJE_PROST 1
  12.  
  13. int main()
  14. {
  15. int n, i, status;
  16. char odgovor[5]="je\0";
  17.  
  18. do {
  19. status=JE_PROST;
  20. printf("Unesite prirodni broj (0 za izlaz): ");
  21. scanf("%d", &n);
  22. if (n == 0) break;
  23.  
  24. for (i = (int)sqrt((double) n) + 1; i-- - 2; )
  25. if (!(n % i)) {
  26. status=NIJE_PROST;
  27. break;
  28. }
  29.  
  30. printf("Broj %d ", n);
  31. status == JE_PROST ? printf("je ") : printf("nije ");
  32. printf("prost broj.\n");
  33. } while(1);
  34.  
  35. return 0;
  36. }
  37.  
  38.