Advertisement
Slyfoxx724

p5.c

Dec 7th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. 1 #include <stdio.h>
  2. 2 #define TRUE 1
  3. 3 #define FALSE 0
  4. 4
  5. 5 _Bool check_if_prime(unsigned int);
  6. 6
  7. 7 int main(void)
  8. 8 {
  9. 9 unsigned int number_of_primes, test_int, input, latest_prime;
  10. 10 _Bool isPrime;
  11. 11
  12. 12 number_of_primes = 0;
  13. 13 test_int = 1;
  14. 14
  15. 15 printf("For which prime number do you want to know the value?\n");
  16. 16 scanf("%u", &input);
  17. 17
  18. 18 do
  19. 19 {
  20. 20 test_int++;
  21. 21 isPrime = check_if_prime(test_int);
  22. 22 if(isPrime == TRUE)
  23. 23 {
  24. 24 number_of_primes++;
  25. 25 latest_prime = test_int;
  26. 26 }
  27. 27 }
  28. 28 while(number_of_primes < input);
  29. 29
  30. 30 printf("The prime number is %u\n", latest_prime);
  31. 31
  32. 32 return 0;
  33. 33 }
  34. 34
  35.  
  36. 35 _Bool check_if_prime(unsigned int test_int)
  37. 36 {
  38. 37 unsigned int div;
  39. 38 _Bool isPrime;
  40. 39
  41. 40 isPrime = TRUE;
  42. 41 for(div = 2; div < test_int; div++)
  43. 42 {
  44. 43 if(test_int%div==0)
  45. 44 {
  46. 45 isPrime = FALSE;
  47. 46 break;
  48. 47 }
  49. 48 }
  50. 49 return isPrime;
  51. 50 }
  52. 51
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement