Digital-Systems-Assignments/Assignment 4/2.c
2019-09-12 08:55:54 +02:00

35 lines
1.2 KiB
C

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int is_prime(int number)
{ /*Function decleration and definition */
int x, temp = 0;
printf("%d is being checked\n",number);
for (x = 2; pow(x, 2) >= number; x++) { /*using for loop to determine if input is not prime*/
printf("%d \t am in loop\n",number);
if (number < 2) { /*If number is 0 or 1, it is not prime*/
temp = 1;
printf("%d is below 2, is not prime\n",number);
}
if (number % x == 0) { /*If remainder of input/x^2 = 0 then the number is not prime*/
temp = 1;
printf("%d has another divisor, is not prime\n",number);
break;
}
}
if (temp == 0) { /*Print out number if the value of temp is not equal to 1 (so it outputs only prime number)*/
printf("%d\n", number);
}
return number;
}
int main()
{
int lower_limit, upper_limit, i; /*Declaring variables*/
scanf("%d%d", &lower_limit, &upper_limit); /*Storing user input in lower and upper limit variables*/
for (i = lower_limit; i <= upper_limit; i++) { /*using for loop to call is_prime function to calculate if the number is prime*/
is_prime(i);
}
return 0;
}