Overview:
In this tutorial, I will teach you What is a Prime Number? and What do you mean by Factors of a number? Then, I will demonstrate How to find prime Numbers within a specific range? Moreover, I will show you the logic to check if the given number is prime or not. After all, I will show a C Program to find prime numbers within range with output. After all, I will explain the internal execution of this program.
Table of contents:
- What is a Prime Number?
- What do you mean by Factor of a number?
- Demonstration to check prime number with some examples
- Main Logic to find prime Numbers within a specific range
- C program to find prime Numbers within a specific range
- Conclusion
What is a Prime Number?
A Prime Number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Examples of the Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, etc.
In other words, Prime numbers are the positive integers having only two factors, 1 and the integer itself.
For example, factors of 8 are 1,2,4, and 8. But factors of 11 are only 1 and 11. Hence, 11 is a prime number but 8 is not the prime number. 1 is never a prime number.
What do you mean by the Factor of a number?
Factors are made up of divisor and dividend. In other words, you can say that the factors are the numbers from whom a number is divisible. For instance, Factors of 23 ia 1 and 23 but 1, 2, 3, 4, 24 are the factors of 24.
Demonstration to check prime number with some examples
Main Logic to check if the given number is prime or not
- Step 1: We remembered that 1 is not prime therefore, Take a for loop with an initial variable i from 2 to 100.
- Step 2: Check if the current number i is prime or not
- Step 3: If i is divisible by any number other than 1 and self then it is not a prime number.
- Step 4: If the number is prime then print
- Step 5: Else exit.
C program to print prime numbers between 1-100 with output
/* C Program to Print Prime Numbers between 1 to 100 using For Loop*/ #include int main() { int number, j, isPrime; printf("All prime numbers between 1 to 100 %d are:\n "); /* Find all Prime numbers between 1 to 100 */ for(number=2; number<=100; number++) { /* Assume that the current number is Prime */ isPrime = 1; /* Check if the current number i is prime or not */ for(j=2; j<=number/2; j++) { /* If i is divisible by any number other than 1 and self * then it is not prime number */ if(number%j==0) { isPrime = 0; break; } } /* If the number is prime then print */ if(isPrime==1) { printf("%d,", number); } } return 0; }
The output of the Program:
Conclusion:
This section shows the explanation of the program with output.
The internal working of this program depends upon the previous demonstration to find prime numbers within the range.
This Program runs a loop from 2 to 100 and checks for each current number within the range that it is prime or not. After the successful execution of the program, it displays the list of all prime numbers from 2 to 100. As we know that 1 is not a prime number so we don't need to check it.