Prime numbers play a crucial role in mathematics and computer science, especially in areas like cryptography, data security, and algorithm design. A prime number is greater than 1 and divisible only by 1 and itself.
Coders'
comments
A
prime number is a lonely star, Code it right, and you’ll shine afar.
Option
1: C Program to Find Prime Numbers in a Given Range
#include
<stdio.h>
#include
<math.h>
//
Function to check if a number is prime
int
isPrime(int n) {
// Numbers less than or equal to 1 are not
prime
if (n <= 1) return 0;
// Check divisibility from 2 to sqrt(n)
for (int i = 2; i <= sqrt(n); i++) {
// If divisible, not a prime number
if (n % i == 0) return 0;
}
return 1;
// Number is prime
}
int
main() {
int start, end;
printf("Enter the range (start and
end): ");
scanf("%d %d", &start,
&end);
printf("Prime numbers between %d and
%d are:\n", start, end);
// Loop through each number in range
for (int i = start; i <= end; i++) {
// Check if number is prime using function
if (isPrime(i)) {
// Print prime number
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Run
Code
Output:
Enter
the range (start and end): 1 10
Prime
numbers between 1 and 10 are:
2
3 5 7
Explanation:
This
program identifies all prime numbers in a specified range, such as 1 to 100. By
iterating through each number in the range and applying the prime-checking
logic, this C program for finding prime numbers between 1 and 100 shows how
loops can be nested for range-based solutions.
Time
Complexity Analysis
Time
Complexity : O(n√n)
Space
Complexity Analysis
Space
Complexity : O(1)
Option
2: C Program for Prime Number Using Function
#include
<stdio.h>
//
Function to check if a number is prime
int
isPrime(int n) {
// Numbers less than or equal to 1 are not
prime
if (n <= 1) return 0;
// Check divisibility from 2 to n-1
for (int i = 2; i < n; i++) {
// If divisible, not a prime number
if (n % i == 0) return 0;
}
return 1;
}
int
main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
// Call function and print result
if (isPrime(n)) {
printf("%d is a prime
number.\n", n);
} else {
printf("%d is not a prime
number.\n", n);
}
return 0;
}
Run
Code
Output:
Enter
a number: 17
17
is a prime number.
Explanation:
This
program encapsulates the prime-checking logic in a user-defined function. It
enhances code readability and reusability, allowing the function to be called
multiple times. This C program for prime number using function is modular and
ideal for implementing in larger projects.
Time
Complexity Analysis
Time
Complexity : O(n)
Space
Complexity Analysis
Space
Complexity : O(1)
Option
3: C Program to Print Prime Numbers up to n
#include
<stdio.h>
#include
<math.h>
//
Function to check if a number is prime
int
isPrime(int n) {
// Numbers less than or equal to 1 are not
prime
if (n <= 1) return 0;
// Check divisibility from 2 to sqrt(n)
for (int i = 2; i <= sqrt(n); i++) {
// If divisible, number is not prime
if (n % i == 0) return 0;
}
return 1;
}
int
main() {
int n;
printf("Enter the limit: ");
scanf("%d", &n);
printf("Prime numbers up to %d
are:\n", n);
// Loop from 2 to n
for (int i = 2; i <= n; i++) {
// Check each number using isPrime function
if (isPrime(i)) {
// Print prime number
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Run
Code
Output:
Enter
the limit: 10
Prime
numbers up to 10 are:
2
3 5 7
Explanation:
This
program finds and prints all prime numbers up to a given limit n. It iterates
through numbers from 2 to n and applies the prime-checking logic. This C
program to print prime numbers up to n helps understand range-based iterations
in combination with conditional checks.
Time
Complexity Analysis
Time
Complexity : O(n√n)
Space
Complexity Analysis
Space
Complexity : O(1)
Option
4: C Program for Prime Number Using Even Number Skipping
#include
<stdio.h>
//
Function to check prime number
int
checkPrime(int num) {
// Numbers less than 2 are not prime
if (num < 2)
return 0;
// 2 is the only even prime number
if (num == 2)
return 1;
// Eliminate other even numbers
if (num % 2 == 0)
return 0;
// Check only odd divisors up to sqrt(num)
for (int i = 3; i <= num / i; i += 2) {
// If divisible, not prime
if (num % i == 0)
return 0;
}
return 1;
}
int
main() {
int
number = 29;
// Call function and print result
if (checkPrime(number)) {
printf("%d is prime",
number);
} else {
printf("%d is NOT prime",
number);
}
return 0;
}
Run
Code
Output:
29
is prime
Explanation:
The
function checkPrime() first handles edge cases by returning 0 for numbers less
than 2 and 1 for 2, while eliminating all other even numbers early to reduce
unnecessary checks.
It
then checks divisibility using a loop that runs only through odd numbers from 3
up to num / i (which works like √num), making the program more efficient than
checking all numbers.
In
main(), the program calls checkPrime() for a given number (29) and prints
whether the number is prime or not based on the function’s return value.
Time
Complexity Analysis
Time
Complexity : O(n√n)
Space
Complexity Analysis
Space
Complexity : O(1)
No comments:
Post a Comment