divisors.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // divisors.c -- nested ifs display divisors of a number
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. int main(void)
  5. {
  6. unsigned long num; // number to be checked
  7. unsigned long div; // potential divisors
  8. bool isPrime; // prime flag
  9. printf("Please enter an integer for analysis; ");
  10. printf("Enter q to quit.\n");
  11. while (scanf("%lu", &num) == 1)
  12. {
  13. for (div = 2, isPrime = true; (div * div) <= num; div++)
  14. {
  15. if (num % div == 0)
  16. {
  17. if ((div * div) != num)
  18. printf("%lu is divisible by %lu and %lu.\n",
  19. num, div, num / div);
  20. else
  21. printf("%lu is divisible by %lu.\n",
  22. num, div);
  23. isPrime= false; // number is not prime
  24. }
  25. }
  26. if (isPrime)
  27. printf("%lu is prime.\n", num);
  28. printf("Please enter another integer for analysis; ");
  29. printf("Enter q to quit.\n");
  30. }
  31. printf("Bye.\n");
  32. return 0;
  33. }