Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Lab exam winter semester 2016/2017.
- Lecture: IE-B1-SO1 (Software construction 1)
- Student: Daniel Dedoukh
- */
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdio.h>
- #include <math.h>
- #define DIM 2
- // Define boolean
- typedef enum
- {
- FALSE = 0, // no need to set value to 0, but it's better for understanding
- TRUE = 1
- }boolean;
- /* Function prototypes */
- int isEven(int);
- boolean isPrimeNumber(int);
- int main(void)
- {
- int num;
- int count = 0;
- // Get user input
- printf("Enter maximum number to test ( positive integer number ) : ");
- while ((scanf("%d", &num) != 1) || (num < 1))
- {
- printf("Invalid input, must be positive integer. Retry: ");
- while (getchar() != '\n')
- continue;
- }
- while (getchar() != '\n')
- continue;
- // Print prime numbers
- printf("Prime numbers in [1, %d]: \n", num);
- for (int i = 0; i <= num; i++)
- {
- if (isPrimeNumber(i) == TRUE)
- {
- printf("%3d ", i);
- count++;
- if ((count % 10) == 0)
- putchar('\n');
- }
- }
- printf("\nThere are %d prime numbers in [1, %d]: \n", count, num);
- getchar();
- return 0;
- }
- int isEven(int a)
- {
- /* Test even numbers */
- if ((a % 2) == 0)
- return TRUE; // if number is even return TRUE
- else if ((a % 2) != 0)
- return FALSE; // if number is not even return FALSE
- return 0;
- }
- boolean isPrimeNumber(int a)
- {
- if (a <= 1)
- return FALSE;
- /* Test even numbers */
- if ((isEven(a) == TRUE) && (a >= 4))
- return FALSE;
- /* Test odd numbers */
- for (int i = 3; i < a / 2; i += 2)
- {
- if ((a % i) == 0)
- return FALSE;
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement